Wordpress - multiple WP Query objects into one?

前端 未结 2 1420
Happy的楠姐
Happy的楠姐 2021-01-14 05:22

In Wordpress it\'s possible to create own WP Querys for the loop. An example is this:

$my_query = new WP_Query(array(\'post_parent\' => 3, \'post_type\' =         


        
2条回答
  •  无人及你
    2021-01-14 05:34

    what you want would translate to a WHERE ... OR ... condition or a UNION in SQL, eg.

    SELECT * FROM posts WHERE (post_parent = 3 AND post_type = 'page') 
      OR (cat = 1 AND post_type = 'post')
    

    or

    SELECT * FROM posts WHERE post_parent = 3 AND post_type = 'page'
      UNION
    SELECT * FROM posts WHERE cat = 1 AND post_type = 'post'
    

    from looking at the source and the way WP constructs SQL from WP_Query(), i don't think this is possible: there is no OR'ing nor UNION of query vars.

    the only thing that comes to my mind is writing a plugin that implements the posts_where filter (applied to the WHERE clause of the query that returns the post array). you could call this plugin with your different WP Querys, and the plugin would get their WHERE parts and could OR them together.

    see also http://codex.wordpress.org/Custom_Queries

提交回复
热议问题