WP_Query | Help me create a search term with an ‘OR’ relation?
I have written a very elaborate script that will essentially allow the user to select from categories, tags and of course the search term. Then run these parameters through a WP_Query.
I can successfully achieve the results I want using the ‘AND’ relation, this works for when the user populates search parameters into all fields; search term, tags & categories. It will also work for leaving one of the three parameters blank, two of the parameters blank and will return all products when all parameters are left blank.
I am then trying to add in an option to use the relation ‘OR’. Where it will allow them to still add in the three search parameters however the results should only have to apply to one of the parameters. This works if the user only selects from tags and categories, however it doesn’t return the correct results if they use the search term parameter.
I believe this to be because the relation is only set to the tags and categories, I need this to be extended to the search term too.
Hopefully you can grasp what I am trying to achieve, in my snippet you can assume all variables populating are correct, I simply wish to ask for help adding the search term as a parameter with the relation ‘OR’. Here is my current WP_Query:
$paged = !empty($_POST['page']) ? $_POST['page'] : 1;
$display_count = !empty($_POST['display_count']) ? $_POST['display_count'] : 9;
$direction = !empty($_POST['direction']) ? $_POST['direction'] : '';
$search_term = !empty($_POST['search_term']) ? $_POST['search_term'] : '';
$search_tags = !empty($_POST['search_tags']) ? $_POST['search_tags'] : '';
$search_categories = !empty($_POST['search_categories']) ? $_POST['search_categories'] : '';
$search_relation = !empty($_POST['search_relation']) ? $_POST['search_relation'] : 'AND';
$offset_modifier = 0;
if ($direction === 'prev') :
$offset_modifier = $paged - 2;
elseif ($direction === 'next') :
$offset_modifier = $paged;
elseif ($direction === 'last') :
$offset_modifier = $paged - 1;
endif;
$offsetcalc = $offset_modifier * $display_count;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => $display_count,
'page' => $paged,
'offset' => $offsetcalc,
'tax_query' => array()
);
if ( $search_term != '' ) :
$args['s'] = $search_term;
endif;
if ( $search_tags != '' ) :
$args['tax_query']['product_tag'] = array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => array($search_tags),
);
if (strpos($search_tags, ', ') !== false) {
$args['tax_query']['product_tag']['relation'] = $search_relation;
}
endif;
if ( $search_categories != '' ) :
$args['tax_query']['product_cat'] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array($search_categories),
);
if (strpos($search_categories, ', ') !== false) {
$args['tax_query']['product_cat']['relation'] = $search_relation;
}
endif;
if ( $search_tags != '' && $search_categories != '' ) :
$args['tax_query']['relation'] = $search_relation;
endif;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
So trying my best to adapt this to have the relation of ‘OR’ on the search term (note the “if($search_term != ”):” and the change to the “$args[‘tax_query’][‘relation’] = $search_relation;” IF statement):
$paged = !empty($_POST['page']) ? $_POST['page'] : 1;
$display_count = !empty($_POST['display_count']) ? $_POST['display_count'] : 9;
$direction = !empty($_POST['direction']) ? $_POST['direction'] : '';
$search_term = !empty($_POST['search_term']) ? $_POST['search_term'] : '';
$search_tags = !empty($_POST['search_tags']) ? $_POST['search_tags'] : '';
$search_categories = !empty($_POST['search_categories']) ? $_POST['search_categories'] : '';
$search_relation = !empty($_POST['search_relation']) ? $_POST['search_relation'] : 'AND';
$offset_modifier = 0;
if ($direction === 'prev') :
$offset_modifier = $paged - 2;
elseif ($direction === 'next') :
$offset_modifier = $paged;
elseif ($direction === 'last') :
$offset_modifier = $paged - 1;
endif;
$offsetcalc = $offset_modifier * $display_count;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => $display_count,
'page' => $paged,
'offset' => $offsetcalc,
'tax_query' => array()
);
if ( $search_term != '' ) :
$args['tax_query']['product_tit'] = array(
'taxonomy' => 'product_tit',
'field' => 'slug',
'terms' => $search_term,
);
endif;
if ( $search_tags != '' ) :
$args['tax_query']['product_tag'] = array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => array($search_tags),
);
if (strpos($search_tags, ', ') !== false) {
$args['tax_query']['product_tag']['relation'] = $search_relation;
}
endif;
if ( $search_categories != '' ) :
$args['tax_query']['product_cat'] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array($search_categories),
);
if (strpos($search_categories, ', ') !== false) {
$args['tax_query']['product_cat']['relation'] = $search_relation;
}
endif;
if ((isset($search_term) && isset($search_tags)) || (isset($search_term) && isset($search_categories)) || (isset($search_tags) && isset($search_categories))) :
$args['tax_query']['relation'] = $search_relation;
endif;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
My changes do not allow for the search term parameter to work, I imagine I am using the wrong methodology or the wrong taxonomy name ‘product_tit’…
Please help me create the search term as a parameter with the ‘OR’ relation and ensure it is narrowed down to the products title only
Can anyone help?
Really appreciative of contributors, Jason.
Leave an answer