Nick Budden
Nick Budden

Reputation: 641

Custom query: multiple custom post types

I am having some trouble pulling multiple custom post types from a Custom Wordpress Query. The reason I am creating a custom Wordpress query rather than using query_posts or WP_query is because I am sorting my posts based on information added by a voting plugin, and have to join that plugin's table, so the built-in queries are not an option.

My question is how can I include multiple custom post types in the same query? At present, my query looks like the following:

$query = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID         = wpostmeta.post_id
    AND wposts.post_status  = 'publish'
    AND wposts.post_type    = 'TWO_POST_TYPES'
    AND post_date       >= '$startdate'
    AND post_date       <= '$enddate'
    GROUP BY wposts.ID
    ";

I am trying to put two different custom post types into the wposts.post_type part, which we can call type1 and type2. What I have already tried is the following, with no luck:

  1. array('type1', 'type2')
  2. 'type1, type2'

I have also tried passing both of these as variables in the query, but also no luck. Would anybody be able to give me a hand?

Upvotes: 1

Views: 3017

Answers (1)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115630

Do you mean this?

AND wposts.post_type IN ('type1', 'type2')

which actually means:

AND ( wposts.post_type = 'type1'
   OR wposts.post_type = 'type2'
    )

Upvotes: 2

Related Questions