WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an easier or more efficient way of querying posts from multiple post types by the date of which they were published?

I read this question here: Using Query Posts With Multiple Post Types And A Taxonomy and the accepted answer.

Although the answer provided probably works, I decided to put a bit of a spin on it in hopes of a more efficient method of retrieving posts from multiple post types using WP_Query(); instead of get_posts();

Here is what I came up with:

private function get_posts(){
        return false;
    global $wpdb;
    $post_ids = array();
    foreach($this->post_types as $post_type){
        $results = $wpdb->get_results($wpdb->prepare(
            SELECT ID
            FROM ".$wpdb->posts."
            WHERE `post_type` LIKE '%%%s%%'
            AND `post_status` = '%s'
            ORDER BY `post_date` DESC
        foreach($results as $result){
            $post_ids[] = $result->ID;
        return false;
        $paged = get_query_var('paged');
    } elseif(get_query_var('page')){
        $paged = get_query_var('page');
    } else{
        $paged = 1;
    $query = new WP_Query(array(
        'post_type' => 'any',
        'post_status' => 'publish',
        'post__in'  => $post_ids,
        'orderby' => 'date',
        'order' => 'DESC',
        'paged' => $paged
        return false;
    return $query->posts;

$this->post_types is just a simple array of post type names which posts should be retrieved from.

If I specify 'post_types' => $this->post_types in my query arguments, it only fetches posts from the post type post, and none of the other custom post types in the array.

When I specify 'post_types' => 'any' it retrieves the posts just fine.

However, if I were to drop 'post__in' => $post_ids and leave 'post_type' => 'any' then it would be retrieving posts made in ANY post type, but if I change to 'post_type' => $this->post_types' then it again, only returns posts from the post type post.

To clear things up. With the code I provided above, it does exactly what I need it to do.

Is there an easier or more efficient way of querying posts from multiple post types?

share|improve this question
'post_type' => $this->post_types should be ok. is your custom post type is queryable, when registering the post type, is query_var set to false somehow ? – Shazzad Dec 13 '13 at 18:44
query_var is set to true. – Michael Ecklund Dec 13 '13 at 19:02
have you tried using comma separated string for post type ? 'post_type' => implode(',', $this->post_types) – Shazzad Dec 13 '13 at 19:23
That doesn't work. – Michael Ecklund Dec 13 '13 at 20:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.