Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(){
    if(!$this->post_types){
        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
            ",
            $post_type,
            'publish'
        ));
        if(!$results){
            continue;
        }
        foreach($results as $result){
            $post_ids[] = $result->ID;
        }
    }
    if(!$post_ids){
        return false;
    }
    if(get_query_var('paged')){
        $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
    ));
    if(!$query->posts){
        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
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.