FacetWP – example of combining results from different queries into a final query

mgibbs189

{{gist file=”query-arguments.php” lang=”php”}}
<?php

// Get “NV” articles
$q1_args = array(
‘post_type’ => array( ‘articles’ ),
‘posts_per_page’ => -1,
‘facetwp’ => false,
‘fields’ => ‘ids’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘article-region’,
‘terms’ => array( ‘nv’ )
)
)
);

// Get all articles
$q2_args = array(
‘post_type’ => array( ‘articles’ ),
‘posts_per_page’ => -1,
‘facetwp’ => false,
‘fields’ => ‘ids’
);

$q1 = new WP_Query( $q1_args );
$q2 = new WP_Query( $q2_args );

// return the final query arguments
return array(
‘post_type’ => array( ‘articles’ ),
‘post__in’ => $q1->posts + array_diff( $q2->posts, $q1->posts ),
‘posts_per_page’ => 10,
‘orderby’ => ‘post__in’,
);
{{/gist}}