Search code examples
phpwordpresscustom-post-type

tyring to combine two wordpress queries into one.


I am trying to display all the posts from my custom post type 'portfolio' with all of the posts that a 'meta_key' of 'cross_post_to_gallery'. I can write a WP_Query that gives me all of the posts from the post type 'portfolio' and a query that gives me all of the post of the type 'post' that have the meta value of 'cross_post_to_gallery'. BUT i don't know how to combine the two...

This is what I have so far. (this will produces post from the post type 'post' but none of the posts from 'portfolio'.)

$args_crosspost_gallery = array(
    'post_type' => 'post',
     'meta_key' => 'cross_post_to_gallery', 
         'posts_per_page' => -1
  ) ;

 $args_portfolio =    array('post_type' => 'portfolio', 'posts_per_page' => -1 );


 $queryportfolio = array(array_merge(  $args_portfolio, $args_crosspost_gallery));  


            $loop = new WP_Query($queryportfolio );

            while ( $loop->have_posts() ) : $loop->the_post();

Could any one please give a point in the right direction...


Solution

  • You shouldn't combine the queries, just the results. So query for each post type with their respected args, and merge the resulting arrays.

    If you want to sort them by date, you can try something like: https://gist.github.com/jcobb/2993853