Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Show member recent post in members loop


  • applegateian
    Participant

    @applegateian

    Hey guys

    The ‘posts’ that my members create are the most important aspect of my site. I’m successfully showing the most recent ‘posts’ by an author on their profile using even though it is out of date

    What I need to do next is show one recent post by each author on our members home page, presumably within members-loop.php

    Is there a way to get this most recent post here?

    Many thanks,

    Ian

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

  • applegateian
    Participant

    @applegateian

    Does anyone know of a way do get most recent post in the members loop for each author? Thanks.


    applegateian
    Participant

    @applegateian

    Sorry to spam, but totally stuck on this, any ideas?


    @mercime
    Keymaster

    @mercime


    applegateian
    Participant

    @applegateian

    Thanks @mercime – however, I’m already successfully showing users posts on their profile – see below. I’m doing this using posts-on-profile but it’s quite out of date:

    What I can’t do, is show the most recent post for each user on the members list (on the members home page) – note the placeholder image on the right for each member:

    Hope someone can help!


    Henry
    Member

    @henrywright-1

    Try putting this in members-loop.php

    <?php
        $user_id = bp_get_member_user_id(); 
        $query = new WP_Query( 'author=' . $user_id );
    
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                //display title
                the_title();
                //display content
                the_content();
            }
        }
    
    //reset for next member
    wp_reset_postdata();
    ?>

    applegateian
    Participant

    @applegateian

    @henrywright-1 that works!

    I need to show the featured image, and link to the profile, so I added the following, but it’s just pasting the image URL, how do I add img src to that and the link to the post?

    <?php
        $user_id = bp_get_member_user_id(); 
        $query = new WP_Query( 'author=' . $user_id );
    
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                //display title
                the_title();
                //display content
                the_content();
                //featured image
                the_field('featuredimage');
            }
        }
    
    //reset for next member
    wp_reset_postdata();
    ?>

    Henry
    Member

    @henrywright-1

    Do you mean this “the_field(‘featuredimage’);” is displaying the feature image URL? If it is, then you can just wrap some HTML around it:

    <?php
        $user_id = bp_get_member_user_id(); 
        $query = new WP_Query( 'author=' . $user_id );
    
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                //display title
                the_title();
                //display content
                the_content(); ?>
           
                <a href="<?php the_permalink(); ?>">
                   <img src="<?php the_field('featuredimage'); ?>" alt="" />
                </a>
           <?php }
        }
    
    //reset for next member
    wp_reset_postdata();
    ?>

    applegateian
    Participant

    @applegateian

    Oh it’s actually THAT simple? Thanks so much, I’m enjoying learning from you guys 🙂


    Henry
    Member

    @henrywright-1

    Yep! not really that hard at all :} Check out the WordPress Codex, you’ll find lots of useful info on there…

    https://codex.wordpress.org/


    applegateian
    Participant

    @applegateian

    OK it works really really well – except, it brings in not just the most recent, but 3 recent posts, can I limit the number?


    Henry
    Member

    @henrywright-1

    You should be able to.

    Try changing

    $query = new WP_Query( 'author=' . $user_id );

    to

    $query = new WP_Query( 'showposts=1&author=' . $user_id );


    applegateian
    Participant

    @applegateian

    Cool @henrywright-1 that’s got it. I found that showposts query, just wasn’t implementing it correctly. I owe you a pint (or 4).


    Henry
    Member

    @henrywright-1

    Great stuff, glad you got it to work!


    applegateian
    Participant

    @applegateian

    Yep, works like a charm 🙂


    4ella
    Participant

    @4ella

    I would like to ask to @henrywright-1
    how could I display 1 latest post from different custom post types, I have several CPT posts as a “portfolio”, “applicant”, “employer”. I would like to display 1 latest post from every custom post type (if exists because for example CPT applicant creates user role applicant, so it is not an employer), I don’t need featured photos etc, only one recent post (custom post type) with permalink to every Custom POST TYPE (where exists obviously).
    For example

    John Doe

    latest post from “portfolio”: John Doe Portfolio
    latest post from CPT “applicant”: John Doe applicant
    latest post from CPT “images”: John Doe’s latest images

    I have installed excellent Buddyblog plugin but it shows me only one post or custom post type, not more than one, so I use it only for simple posts, but I also want to show custom post types on member’s profiles.


    Henry
    Member

    @henrywright-1

    @4ella

    $query = new WP_Query( 'post_type=portfolio' );

    More info on how you can use the WP_Query class:

    https://codex.wordpress.org/Class_Reference/WP_Query

Viewing 16 replies - 1 through 16 (of 16 total)
  • The topic ‘[Resolved] Show member recent post in members loop’ is closed to new replies.
Skip to toolbar