0

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?

1
  • 1
    That should function, and yes - that if else should do what you want, and it's a reasonable way to do it. Commented Mar 29, 2016 at 21:48

2 Answers 2

1

I always build my own loop whenever I needed a custom query.

You can probably do it like this,

# First Argument
$args = array(
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'industry',
            'terms'    => $user_input,
        ),
        array(
            'taxonomy' => 'format',
            'terms'    => $user_input2,
        ),
    ),
);
# Second Argument
$lessargs = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'industry',
            'terms'    => $user_input,
        ),
    ),
);

$query = new WP_QUery( $args ); //Run First Query

$posts = $query->get_posts(); //Get Post of first Query

if ( !$posts ) { //If no post on First query Run Second Query
    $query = new WP_QUery( $lessargs );
    $posts = $query->get_posts(); //Get Post of second query
}
if ( !$posts ) return 'No Result Found'; //stop execution if no results
foreach( $posts as $post ) { //Loop through each result
        _e( $post->post_title ); // Echo the title
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

1

I would do it slightly differently, just to cater for the fact that neither query will return any rows:

$the_query = new wp_query($args);
if ($the_query->have_posts()) {
    while ($the_query->have_posts()) {
        the_post();
        // Return Query Results
    }
} else {
    $the_query = new wp_query($lessargs);
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            the_post();
            // Return Second Query Results
        }
    } else {
        echo '<p>No posts found.</p>';
    }
}

Note you have a typo with your $lessargs variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.