Reputation: 389
I currently have a search bar on my category page which will allow the search of either the product title (custom post type) or a taxonomy of brand. I can get both of these to work independently, passing in either arguments to the WP_Query, however if I attempt to array merge them both together, neither work.
What I have is:
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$cats = array(
'taxonomy' => 'productscat',
'field' => 'id',
'key' => 'name',
'terms' => $post_category,
);
$args = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'tax_query' => $cats,
'posts_per_page' => -1,
);
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search,
),
),
'posts_per_page' => -1,
);
$merged = array_merge($args, $args2);
$search_query = new WP_Query($merged);
if ($search_query->have_posts()) :
And so on with the loop; So what am I missing with the merge arguments?
Essentially i am using 's' => $search
only in one of the arrays to search the post name, which I do not need (or believe i need) in the brands taxonomy array
Upvotes: 1
Views: 487
Reputation: 11282
You can combine queries like this. You can add multiple tax_query
.
$search = sanitize_text_field($_POST['productsearch']);
$post_category = $_POST['post_cat'];
$args2 = array(
'post_type' => 'products', //custom post type
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC',
's' => $search,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $search
),
array(
'taxonomy' => 'productscat',
'field' => 'id',
'terms' => $post_category
)
)
);
$search_query = new WP_Query( $args2 );
if ( $search_query->have_posts() ) :
endif;
Upvotes: 1