Help with adding pagination to custom wp_query

I have custom wp_query used in buddypress to list all posts of the author (taken from the profile user currently visits). I tried to follow other topics but I’m failing miserably.

Code below:

    ?php // Display posts in user profile

    // Construct new user profile tab

add_action( 'bp_setup_nav', 'add_profileposts_tab', 100 );

function add_profileposts_tab() {
 global $bp;
 bp_core_new_nav_item( array(
 'name' => 'Publikacje',
 'slug' => 'publikacje',
 'screen_function' => 'bp_postsonprofile',
 'default_subnav_slug' => 'Moje publikacje', 
 'position' => 35
 )
 );
}

// show 'Posts' tab once clicked

function bp_postsonprofile() {
 add_action( 'bp_template_content', 'profile_screen_posts_show' );
 bp_core_load_template( apply_filters( 'bp_core_template_plugin',     'members/single/plugins' ) );
}

// query the loop and get the template

function profile_screen_posts_show() {

$user_id = bp_displayed_user_id(); 
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( 'author=" . $user_id );





 ?
    ?php if ( $query->have_posts() ) : ?
    ?php while ( $query->have_posts() ): $query->the_post(); ?
        <div id="post-<?php the_ID(); ?> blog_list" <?php post_class(); ?>>
                <div class="container-wrapper blog-list">
                <div class="post-tittle"><h2 class="posttitle"> <a href="https://wordpress.stackexchange.com/questions/291261/<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> </h2></div>
            </br>
            <div class="post-wrapper">

                <div class="miniaturka post-left">
                ?php if ( function_exists( "has_post_thumbnail' ) && has_post_thumbnail( get_the_ID() ) ):?

                    <div class="post-featured-image left">
                       <a href="https://wordpress.stackexchange.com/questions/291261/<?php the_permalink(); ?>"><?php  the_post_thumbnail('medium');?></a>
                    </div>
               ?php endif;?
            </div>
            <div class="post-content post-right">

                <p class="author class"><?php echo get_the_date(); ?></br><?php printf(get_the_category_list( ', ' ) ); ?></p>
                </br>
                <div class="entry">

                    <?php the_excerpt(); ?>
                    <span class="comments"><?php comments_popup_link(); ?></span>
                </div>


            </div>

            </div>

        </div>
        </div>
 ?php endwhile; ?

 ?php else: ?
 ?php wp_reset_query(); ?

 <div id="message" class="info">
 <p><?php bp_displayed_user_username(); ?> jeszcze niczego nie napisał         </p>
 </div>

    ?php endif; wp_reset_postdata();?    ?php

}
?

I tried using (sorry for awful formatting):

$query = new WP_Query(array('author="=>$user_id, "posts_per_page'=>20, 'paged'=>$paged));

But while it works (without pagination, of course) I get it listing ALL posts, no matter the author. What I am doing wrong (other than being super bad at this)?

2 Answers
2

Personally I would do this after fetching the user id:

echo '<pre>';
var_dump($user_id);
echo '</pre>';

To check it is getting the $user_id as expected. (int)
This should be a comment, but I am lacking reputation.

Leave a Comment