Skip to:
Content
Pages
Categories
Search
Top
Bottom

Buddypres query_posts by user

  • Avatar of yan.odnoralov
    yan.odnoralov
    Member

    @yanodnoralov

    In a custom user tab I’m trying to display that users posts. (displayed user, not logged in user).

    I found this code on a forum but it’s not working for me. It should pull the ‘displayed users’ posts only, instead it shows all posts. How Can I make it pull only posts by the “displayed user”?

    `query_posts( ‘author=’ . $bp->displayed_user->id );

    // the Loop
    while (have_posts()) : the_post();
    the_content( ‘Read the full post »’ );
    endwhile;
    `

    What am I doing wrong?

    Thanks a ton!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Avatar of Hugo
    Hugo
    Moderator

    @hnla

    I should think it’s just a syntax error your meant to pass a string but you’ve closed it out after =’ to parse the BP user id but not opened closed again try ” on the end.

    WP_Query may be better for this but regardless you probably ought to run wp_reset_query() after the loop

    The WP Codex will be your friend here though – it is more a WP related matter than BP.

    Avatar of yan.odnoralov
    yan.odnoralov
    Member

    @yanodnoralov

    @Hugo Thanks for the response. Reading this article and a few others, (http://stackoverflow.com/questions/4907350/php-help-with-using-variable-inside-code) I have to close out the tag after author=

    I searched WP Codex in and out… How would I call or filter my query by the buddy press “displayed_user” variable?

    Thanks so much!

    Avatar of @ChrisClayton
    @ChrisClayton
    Participant

    @chrisclayton

    @yanodnoralov – You do have `global $bp;` inside the function, right? (Just checking, sometimes it’s the simplest things that get overlooked)

    Avatar of yan.odnoralov
    yan.odnoralov
    Member

    @yanodnoralov

    @ChrisClayton I’m not so hot with php.. What exactly would my global code be in functions?

    Avatar of Hugo
    Hugo
    Moderator

    @hnla

    A ‘global’ makes a variable available in different scopes – a function creates a new scope and code or variables that exist outside the function need to be passed through:

    `global $bp;`

    However if you are on a bp screen? you likely don’t need to pull out parts from the $bp global you should be able to use ‘bp_displayed_user_id()’

    Avatar of yan.odnoralov
    yan.odnoralov
    Member

    @yanodnoralov

    Ok, I believe I am on a bp screen. Here is my full code. This is the “functions.php” in my child theme. It displays 3 custom tabs and in the last tab is where y code struggle is. I think @ChrisClayton is right, I just need to have global $bp; inside the function. I just don’t know how I could code that. I appreciate the help guys, this has literally kept me up 5 hours of last night!

    Avatar of yan.odnoralov
    yan.odnoralov
    Member

    @yanodnoralov

    Hmmm, buddypress won’t let me post the code. Telling me I already posted it… Here is my child-theme’s full functions.php code: http://pastebin.com/ADbF82xL

    Avatar of yan.odnoralov
    yan.odnoralov
    Member

    @yanodnoralov

    *bump

    guys, any help here would be really appreciate.

    Avatar of @ChrisClayton
    @ChrisClayton
    Participant

    @chrisclayton

    `function my_profile_page_function_to_show_screen333_content() {
    global $bp;

    query_posts( ‘author=’ . $bp->displayed_user->id );

    // the Loop
    if (have_posts()) : the_post();
    the_content( ‘Read the full post »’ );
    endif;
    }
    `

    If you don’t tell it to use the global data, then it will assume you have defined it inside the function (and since you haven’t defined it, returns it false)

    But, as @hnla said – you can just use the function (which will do the same thing as the above, with less code and easier to use)

    `function my_profile_page_function_to_show_screen333_content() {
    query_posts( ‘author=’ . bp_displayed_user_id() );

    // the Loop
    if (have_posts()) : the_post();
    the_content( ‘Read the full post »’ );
    endif;
    }
    `

    Avatar of yan.odnoralov
    yan.odnoralov
    Member

    @yanodnoralov

    Wow, perfect! So simple and works @ChrisClayton! Thanks a bunch guys!

    Cheers

Viewing 10 replies - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.