Wordpress: Show Posts of specific user
I recently ran in to a stumbling block of showing specific posts by a user.
I had created a CPT for 'People' and on that page I wanted to show that users recent posts.
I was using a meta box to add the username of the person (which the posts would be associated with) and then used the handy getuserby() function to find out the users id from the username.
//extracting the username meta box i set up
<?php $userid = (types_render_field("user-id", array("output" => "html"))); ?>
<?php $user = get_user_by('login', $userid);
//grab the ID from the username
$theuser = $user->ID; ?>
<?php query_posts("author=$theuser&posts_per_page=6" );?>
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
<?php the_title(); ?></a>
</li>
<?php endwhile; else: ?>
</ul>
<p><?php _e('No Recent Posts by this chap'); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
and that's all there is to it really.
Written by Daniel Hodkinson
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Author
Authors
danhodkinson
2.779K
anthonylevings
1.528K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#