[wp-hackers] how to query all posts from post type and some posts from another post type?

Michael Van Winkle mike at mikevanwinkle.com
Mon May 7 16:07:37 UTC 2012


Time to learn some mysql and do a custom select query.
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

Try something like this replacing the slug below ('slug-for-my-term')
for whichever you need to query on. Please beware that if you populate
this query dynamically you will need to user the $wpdb->prepare method
to make certain it is secure.

SELECT * from wp_posts p
LEFT JOIN wp_term_relationships rel ON rel.object_id = p.ID
LEFT JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = rel.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
WHERE p.post_status = 'publish' AND ( ( p.post_type = 'post' AND
t.slug = 'slug-for-my-term' AND tt.taxonomy = 'category') OR
p.post_type = 'fdp-post' )
GROUP BY p.ID
ORDER BY p.post_date,DESC

Cheers,

Mike
On Mon, May 7, 2012 at 8:39 AM, Jackson Whelan <jw at jacksonwhelan.com> wrote:
> On 5/7/12 11:16 AM, Steve Taylor wrote:
>>>
>>> You can use an array for the post_type parameter in WP_Query, ie:
>>>
>>>    $query = new WP_Query( array( 'post_type' =>  array( 'post',
>>> 'fdp-post' )
>>> ) );
>>
>> The issue then is, how to do a tax_query that for only restricting one
>> post type by category. Probably not possible
>
> Apologies, I totally missed that part of the OP's question.
>
> I believe you're right that the tax_query will apply to both post types.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



-- 
Mike Van Winkle
708-289-3136
mike at mikevanwinkle.com
http://www.mikevanwinkle.com
http://www.twitter.com/mpvanwinkle
http://www.facebook.com/mpvanwinkle


"All excellent things are as difficult as they are rare." -Spinoza


More information about the wp-hackers mailing list