How to display recent posts added in several custom post types

So I have several custom post types on my site and would like to have a place on my home page that will show the most recent 6 posts from 3 different custom post types. I have looked at the setup on How to display recent posts added in custom post types but can not figure out how to have it pull from more then 1 post type.

<h2>Recent Posts</h2>
<ul>
<?php
$recent_posts =  wp_get_recent_posts(array('post_type'=>'books','stories','movies'));
foreach( $recent_posts as $recent ){
    echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
}
?>
</ul>

1 Answer
1

You can pass multiple custom post types in an array for ‘post_type’, as in:

'post_type' => array('books', 'stories', 'movies')

Your code seems to try to do this, but the syntax is a bit off, it should be:

$recent_posts = wp_get_recent_posts(
    array(
        'post_type' => array( 'books', 'stories', 'movies' ),
    )
);

You can find more details about every available args in the Documentation for WP_Query.

Leave a Comment