Query a custom post type by 2 terms of a custom taxonomy, while getting half from each term

I have a custom taxonomy named “level” which has 2 terms: basic and advanced.
This taxonomy is connect to custom post type named “test”

What I want to do is to query all the tests, and to get half posts having “basic” assign to them, and half with “advanced” assign to them (will be better if instead of half, I could use some variable).

The naive way is of course to make 2 queries, one for each term:

$args = array(
‘post_type’ => array( TEST_POST_TYPE ),
‘posts_per_page’ => -1,
‘post_status’ => ‘publish’,
‘tax_query’ => array(
array(
‘taxonomy’ => DIFFICULTY_TAXONOMY,
‘field’ => ‘term_id’,
‘terms’ => BASIC_DIFFICULTY_LEVEL_TERM_ID ,
),

)
);

$questions_basic = new WP_Query( $args );
$args[‘tax_query’][0][‘terms’] = ADVANCED_DIFFICULTY_LEVEL_TERM_ID;
$questions_advanced = new WP_Query( $args );

Is possible to make such a query using WP_QUERY or I have to create a custom SQL query?

Read more here:: Query a custom post type by 2 terms of a custom taxonomy, while getting half from each term

Leave a Reply

Your email address will not be published. Required fields are marked *