Does anyone know of a way do get most recent post in the members loop for each author? Thanks.
Sorry to spam, but totally stuck on this, any ideas?
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!
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();
?>
@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();
?>
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();
?>
Oh it’s actually THAT simple? Thanks so much, I’m enjoying learning from you guys 🙂
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/
OK it works really really well – except, it brings in not just the most recent, but 3 recent posts, can I limit the number?
You should be able to.
Try changing
$query = new WP_Query( 'author=' . $user_id );
to
$query = new WP_Query( 'showposts=1&author=' . $user_id );
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).
Great stuff, glad you got it to work!
Yep, works like a charm 🙂
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.
@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