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="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> </h2></div>
            <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="<?php the_permalink(); ?>"><?php  the_post_thumbnail('medium');?></a>
               ?php endif;?
            <div class="post-content post-right">

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

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



 ?php endwhile; ?

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

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

    ?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)?


Personally I would do this after fetching the user id:

echo '<pre>';
echo '</pre>';

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

  • Did that and it "dumps" the correct user_id (currently viewed profile). Yet, it ignores it somehow and shows posts by all the users, not only the one I want. Any idea what's wrong? – user2143561 Jan 18 '18 at 22:06

Ok I figured it out. I was dumb. After few more iterations I settled on this version:

 $user_id = bp_displayed_user_id(); 
 $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $args = array(
'posts_per_page'   => 10,
'post_type'        => 'post',
'author'           => $user_id,
'paged'            => $paged,
$query = new WP_Query($args);

My main problem was that I tried using 'author=' =>$user_id, which made it cycle through all post by all authors.

Maybe that will help somebody.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.