I'm going to have a WP_Query run with arguments based on user input. The user can select multiple categories/terms and the query will filter based on the AND boolean.
// main arguments
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
array(
'taxonomy' => 'format',
'terms' => $user_input2,
),
),
);
// less specific arguments
$lessargs = array(
'tax_query' => array(
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
),
);
If no results are returned in the first query, I want to run the second query with less specificity ($lessargs). I know I need to use if/else statements but I don't know the correct way to do this within the loop. Example:
<?php $the_query = new WP_Query( $args ); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : the_post(); ?>
// Return Query Results
<?php endwhile; ?>
<?php else : ?>
<?php $the_second_query = new WP_Query( $less_args ); ?>
<?php while ($the_second_query->have_posts()) : the_post(); ?>
// Return Second Query Results
<?php endwhile; ?>
<?php endif; ?>
Is this the proper way to conditionally call queries if the previous query returns empty?
if elseshould do what you want, and it's a reasonable way to do it.