0

I have wp_query including custom taxonomy terms which works if all the fields have values. Otherwise, it is giving 0 results.

This is the form that is used to get the taxonomy terms. enter image description here

Based on the submission data WP_Query needs to build. In my case, these taxonomies either can have values and can be nulled as well. Can browse with even one taxonomy or without choosing any.

This is my code so far. This is working really fine if all three fields have selected values. Otherwise, this is not working properly. Need to combine all taxonomies for the results.

This is my form for submission:

<form method="get" action="<?php echo esc_url(home_url('/books/')); ?>">
    <select name="book-category" class="form-select" id="book-category">
        <option value="" selected disabled hidden>Book Category</option>
        <option value="">Category 01</option>
        <option value="">Category 02</option>
        <option value="">Category 03</option>
    </select>
    <select name="publishing-year" class="form-select" id="publishing-year">
        <option value="" selected disabled hidden>Publishing Year</option>
        <option value="">2022</option>
        <option value="">2021</option>
        <option value="">2020</option>
    </select>
    <select name="print-status" class="form-select" id="print-status">
        <option value="" selected disabled hidden>Print Status</option>
        <option value="">In Print</option>
        <option value="">Not in Print</option>
    </select>
    <input type="submit" value="Search" />
</form>

This is how I am assigning each submission into variables.

$book_category = $_GET['book-category'];
$publishing_year = $_GET['publishing-year'];
$print_status = $_GET['print-status'];

And this is the WP_Query I am trying to get the results.

$book = new WP_Query(array(
    'post_type'=>'book',
    'posts_per_page'=>4,
    'tax_query'=> array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'book_category',
            'field'    => 'slug',
            'terms'    => $book_category,
        ),
        array(
            'taxonomy' => 'publishing_year',
            'field'    => 'slug',
            'terms'    => $publishing_year,
        ),
        array(
            'taxonomy' => 'print_status',
            'field'    => 'slug',
            'terms'    => $print_status,
        ),
    ),
));

Any support to get this working?

1
  • I would note that settings posts_per_page to -1 can lead to bad situations. Don't set it to unlimited, set it to a value you know you will never reach but can safely handle, e.g. 150. Otherwise you open yourself to loading more results than your server can ever handle. Also consider using the standard search mechanism instead with a pre_get_posts or using the URL parameters for better performance and compatibility
    – Tom J Nowell
    Feb 3, 2022 at 10:55

1 Answer 1

1

To achieve this you need to conditionally add the clauses, right now you always have all 3, even if only 2 are selected.

So instead pass in a variable:

$tax_queries = [
    'relation' => 'AND',
];

Notice I used the modern [] syntax for arrays instead of array(), and I added the relation field as we always want that.

Then, add each clause by testing those variables, e.g. for the book category:

if ( ! empty( $_GET['book-category'] ) ) {
    $tax_queries[] = [
        'taxonomy' => 'book_category',
        'field'    => 'slug',
        'terms'    => $_GET['book-category'],
    ];
}

This would also be a good time to do any trimming or processing to remove trailing spaces etc.

Do this again for the other 2 clauses/fields. Then finally, use the variable in the query:

    'tax_query'=> $tax_queries,
5
  • Thank you for your quick response. Actually, I want to get a merged result. Ex: If someone selects these options Book Category->A, Publishing Year->2018, and Print Status->Printing it needs to show which matches all three results. If selects two of them, so need to show results that match those two selected options. The relation AND or XOR not working. Feb 3, 2022 at 11:14
  • ooh, then you can't just put all 3 clauses in there all the time, you have to check if the user selected something in the dropdown and conditionally add that part to the array. Sadly that's not something that I could help with as your form isn't code, it's just a screenshot (screenshots are minimally helpful, they don't provide a lot of information)
    – Tom J Nowell
    Feb 3, 2022 at 14:04
  • I have updated the question with the form the get variables. Feb 4, 2022 at 9:37
  • @user2584538 thanks, I've rewritten the answer based on your updates, it should be much more useful now
    – Tom J Nowell
    Feb 4, 2022 at 17:47
  • 1
    Many thanks, Tom. This is really working great. Feb 6, 2022 at 15:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.