Combine two separate wp queries into one single query for search - php

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

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;

Related

Wordpress array for product and post on same page

I use the array to show all products and posts on the same page:
$args = array(
'post_type' => array( 'product', 'post' ),
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC'
);
$custom_query = new WP_Query($args);
Now I want to filter posts 'cat' => '82,85' and products 'product_cat' => 96.
I have added the code snippes to my code, but after no post or products are shown.
$args = array(
'post_type' => array( 'product', 'post' ),
'cat' => '82,85',
'product_cat' => 96,
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC'
);
$custom_query = new WP_Query($args);
How should I modify my code that both post types and all posts and products within the categories will be displayed?
Thank you,
Mags
You can do that by using tax_query.
$args = array(
'post_type' => array('product', 'post'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => ['82', '85'],
),
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => ['96'],
),
),
'posts_per_page' => 999,
'orderby' => 'meta_value_num',
'meta_key' => 'score_value',
'order' => 'DESC',
);
$custom_query = new WP_Query($args);

Combine two or more OR relations in WP Query? [duplicate]

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
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;

WP_Query tax_query multiple taxonomies and terms

I'm having a bit of trouble returning posts with multiple taxonomies and terms. Hoping someone with far more knowledge than myself can help me understand.
I'm using a select menu to populate the select options with taxonomy information of a page (in this case, the Products page). All is good with a single taxonomy and term in the tax_query but as soon as I try to use an array to pass multiples, i'm no longer returning anything. This seems simple enough but I'm missing something. Any ideas?
Here is what I'm working with:
$producttype = $_GET['ProductType'];
$businessunit = $_GET['BusinessUnit'];
$products = new WP_Query( array(
'post_type' => 'products',
'posts_per_page' => 15,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'producttype',
'field' => 'name',
'term' => $producttype
),
array(
'taxonomy' => 'businessunit',
'field' => 'name',
'term' => $businessunit
)
)
)
You error is a key array tax_query => term should be terms
$producttype = $_GET['ProductType'];
$businessunit = $_GET['BusinessUnit'];
$products = new WP_Query( array(
'post_type' => 'products',
'posts_per_page' => 15,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'producttype',
'field' => 'name',
'terms' => $producttype
),
array(
'taxonomy' => 'businessunit',
'field' => 'name',
'terms' => $businessunit
)
)
Reference wordpress

Get all products from multiple categories (Woocommerce/Wordpress)

I want to display all products from multiple categories at once.
When i want to display all products from one category my $args array looks like this:
$args = array(
'post_type' => 'product',
'product_cat' => 'backpacks',
'orderby' => '_sku'
);
I remember that I can simply make an array inside my $args:
$args = array(
'post_type' => 'product',
'product_cat' => array(
'backpacks','accessoires',
),
'orderby' => '_sku'
);
But it gives me the following error:
Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\live\wp-includes\formatting.php on line 4312
I know this is a simple thing but i cant figure out why its not working.
Thanks for any help!
Please try below snippet.
$sortcolumn = 'ID';
$prod_categories = array(12, 17); //category IDs
$product_args = array(
'numberposts' => -1,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'), //skip types
'orderby' => $sortcolumn,
'order' => 'ASC',
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Found a simple way to do it
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'backpacks'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'accessoires'
)
),
);

How to display custom post types related to a specific taxonomy using WordPress

I am trying to display all custom post types of a specific taxonomy in WP.
Here is the code:
<?php
$args = array(
'post_type' => 'team',
'orderby' => 'rand',
'posts_per_page' => -1
);
$trit = new WP_Query($args);
while ($trit->have_posts()) : $trit->the_post();
$post_id = get_the_ID();//POST ID
$terms = get_the_terms($post->ID, 'tax_members' );
...
endwhile;
The script above displays all members of all taxonomies (tax_members). My goal is to display only the members of a specific category...for example: players.
How can I call the taxonomy players inside
$terms = get_the_terms($post->ID, 'tax_members' );
<?php
$queryArr = array(
'post_type' => 'team',
'orderby' => 'rand',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax_members',
'field' => 'slug',
'terms' => 'players',
'operator' => 'IN'
),
),
);
$trit = get_posts( $queryArr );
?>
Try this
$args = array(
'post_type' => 'team',
'orderby' => 'rand',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax_members',
'terms' => array('players'),
)
)
);

Categories