最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - how to get this tax_query working?

programmeradmin1浏览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.

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?

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.

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?

Share Improve this question edited Feb 4, 2022 at 9:36 user2584538 asked Feb 3, 2022 at 10:38 user2584538user2584538 1271 silver badge9 bronze badges 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 Commented Feb 3, 2022 at 10:55
Add a comment  | 

1 Answer 1

Reset to default 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,
发布评论

评论列表(0)

  1. 暂无评论