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

Lionel POINTET lionel.pointet at globalis-ms.com
Mon May 7 16:14:49 UTC 2012


I think custom SQL queries are in most cases a bad idea. You should use it
only if you really need it.

The drawbacks are that you don't rely on the WordPress abstraction methods
so you don't prevent some future changes on the database.
Plus, you don't use actions and filters placed by the WordPress core, so
plugins or themes you use won't be able to hook on them, and you probably
won't have the custom functionalities provided by those plugins / themes.

In case you *really *want and need to do this into one query, use the most
appropriate filter (like 'posts_where' or others) to re-write the tinyer
portion of the WP_Query.

2012/5/7 Michael Van Winkle <mike at mikevanwinkle.com>

> 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
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list