WP_Query | Tax_Query Relation | Unable to use 'OR' as it then allows all products, help me finish my query?

I began slightly baffled and have slowly degraded to slightly going insane as I am building this wp_query.Admittedly it

I began slightly baffled and have slowly degraded to slightly going insane as I am building this wp_query.

Admittedly it is a very complex query and there is a lot of additional code that has brought me up to this point so here is the code that is key to helping me resolve to an answer, the wp_query:

$args = array(
    'post_type'         => 'product',
    'post_status'       => 'publish',
    'orderby'           => 'menu_order',
    'order'             => 'ASC',
    'posts_per_page'    => $display_count,
    'page'              => $paged,
    'offset'            => $offsetcalc,
    's'                 => $search_term,
    'tax_query'         => array(
        'relation'          => $search_relation,
        array(
            'taxonomy'          => 'product_tag',
            'field'             => 'slug',
            'terms'             => array($search_tags),
            'operator'          => $search_relation
        ),
        array(
            'taxonomy'          => 'product_cat',
            'field'             => 'slug',
            'terms'             => array($search_categories),
            'operator'          => $search_relation
        )
    )
);

$the_query = new WP_Query( $args );

Variables are equal to:

$display_count = 9.
$paged = page number.
$offsetcalc = number of products to offset based on calculation done using display count and page.
$search_term = search term entered by user.
$search_relation = user selected string value 'AND' / 'OR'.
$search_tags = tags selected by user.
$search_categories = categories selected by user.

As you can see I am gathering values decided by a user and generating different results based on their inputs.

One of the options I am trying to offer them is the ability to 'show results if the result applies to one of the many options they select' or 'only show results that apply to all the options they select'.

This always works if the user populates the search term field 's'.

This always works if they have the relation set to 'AND'.

However, when the relation is set to 'OR' while also leaving the search term 's' blank, it returns every single result possible.

Please can someone help me successfully implement this 'AND' / 'OR' relation into my wp_query when the search term 's' is left blank?

Massive thanks to contributors, Jason.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744734365a4590652.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信