0

Is there a plugin or code of sort that can display the last posts in a custom post type of a blog author? I know there are plugins/codes to display the last posts of the author, but I want to display only their posts in a particular post type I created.

For example, the author posted in the Posts section a blog post. Underneath the blog post, I want to show his last five posts not in the Posts section, but in the Books section, which is a custom post type I made.

1

You will need to use a WP Query to search for posts of that category.

It will be something like:

 $author = get_the_author(); // defines your author ID if it is on the post in question

 $args = array(
                 'post_type' => 'book',
                 'post_status' => 'publish',
                 'author'=>$author,
                 'posts_per_page' => 5, // the number of posts (books) you'd like to show
                 'orderby' => 'date',
                 'order' => 'DESC'
                 );
 $results = new WP_Query($args);
//    '<pre>'.print_r($results).'</pre>'; // This is a useful line to keep - uncomment it to print all results found - it can then be used to work out if the query is doing the right thing or not.
while ($results->have_posts()) {
$results->the_post();
the_title();
echo '</hr>'; // puts a horizontal line between results if necessary 
}

wp_reset_postdata(); //re-sets everything back to normal       

}

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.