Merge 2 woocommerce product queries - wordpress

I have the following code:
add_action( 'woocommerce_product_query', array($this, 'b2bking_hide_products_category_visibility') );
function b2bking_hide_products_category_visibility($q ){
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $visiblecategories,
'operator' => 'IN'
);
$q->set( 'tax_query', $tax_query );
// Set query to only select products that are in "default" category mode, not manual visibility mode
$q->set('meta_query', array(
array(
'key' => 'b2bking_product_visibility_override',
'value' => 'default',
)
));
/* SEPARATE META QUERY
$q->set('meta_query', array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'b2bking_group_'.$currentusergroupidnr,
'value' => '1'
),
array(
'key' => 'b2bking_user_'.$currentuserlogin,
'value' => '1'
)
),
array(
'key' => 'b2bking_product_visibility_override',
'value' => 'manual',
)
));
*/
}
}
I currently have (tax query AND meta query)
I would like to have (tax query AND meta query) OR (separate meta query)
How can I achieve this? Can it be done within the "woocommerce_product_query" action? If so, what is the right syntax?
Thank you

I figured it out:
You use
$q->set('post__in', $postIDs);
This makes WooCommerce show the exact array of products in the $postIDs array;
The final code looks like this:
add_action( 'woocommerce_product_query', array($this, 'your_product_function') );
function your_product_function(){
/* MAKE YOUR 2 CUSTOM PRODUCT QUERIES AS WP QUERIES */
$queryA = new WP_Query($queryAparams);
$queryB = new WP_Query($queryBparams);
$postIDs= array_merge($queryA->posts,$queryB->posts);
/* IMPORTANT, if the ARRAY is EMPTY, ALL PRODUCTS WILL BE SHOWN. A quick bypass is to pass an array of invalid ids as you can see below */
if(!empty($allTheIDs)){
$q->set('post__in',$postIDs);
} else {
$q->set('post__in',array('invalidid'));
}
}
Very important is to pass 'fields'->'ids' in the parameters to get only IDs
this is how the individual queries look like
$queryBparams = array(
'posts_per_page' => -1,
'post_type' => 'product',
'fields' => 'ids',
'meta_query'=> array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'b2bking_group_'.$currentusergroupidnr,
'value' => '1'
),
array(
'key' => 'b2bking_user_'.$currentuserlogin,
'value' => '1'
)
),
array(
'key' => 'b2bking_product_visibility_override',
'value' => 'manual',
)
));

Related

How to display only instock products in all of my site? Woocommerce + Wordpress

How to add more values to 'value' => 'noproduzione', ?
If i add 'value' => array('noproduzione','10days'), functions.php does not give me error but on frontend i receive error Warning : trim() expects parameter 1 to be string, array given in
add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
if ( ! is_admin() ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => '!=',
);
}
return $meta_query;
}
Complex (nested, multiple relation) queries have been supported since Wordpress 4.1.
You can then achieve a more complex query like this:
$meta_query[] = array(
'relation' => 'OR',
array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => '!=',
),
array(
'key' => '_stock_status',
'value' => '10days',
'compare' => '!=',
),
);

WP Query: How to get posts from a specific author OR those with a specific meta value

I would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!

How to give OR relation in tax_query and meta_query Wordpress

I want give OR relation between tax_query and meta_query:
$post_args = array('post_type' => 'post',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'client_segment',
'value' =>$client ,
'compare' => 'IN',
),
array(
'key' => 'filtered_date',
'value'=> array($start_date, $end_date),
'compare' => 'BETWEEN'
)
),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term_id,
'operator' => 'IN')
),
);
$posts = new WP_Query($post_args);
What is wrong with my code?
Put your meta_query after the tax_query and wrap the WP_Query with 'get_meta_sql' filter, then replace first AND with OR, as below:
add_filter('get_meta_sql', 'filter_query', 10, 1);
$posts = new WP_Query($post_args);
remove_filter('get_meta_sql', 'filter_query');
Then the filter function, which replaces the first AND with OR:
function filter_query($sql){
$pos = strpos($sql['where'], 'AND');
if ($pos !== false) {
$sql['where'] = substr_replace($sql['where'], 'OR', $pos, strlen('AND'));
}
return $sql;
}
This is faster and saves you the time filtering the duplicates after merging the queried posts.
There is no way to give OR relation between two queries, but what you can do to make two separate queries and then merge the results of them.
Because basically you need results of both of these queries.
You can find solution
how to merge 2 queries in this post

List child pages only if specific custom field is not empty in those pages

So I have this function to list all child pages and I want it to don't list those elements which does not have custom field "role" or if this custom field is empty. I've tried different things but even if I type value or meta value "test" all pages are listed. There's something wrong with this query meta_key is working perfectly but meta_value doesn't work.
My function code is this:
function list_child_pages() {
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'role',
'meta_value' => 'test',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
return list_childs();
} else
$string="team members were not found";
return $string;
}
'meta_query' => [
'relation' => 'AND',
[
'key' => 'role',
'value' => 'test',
'compare' => '='
]
],
I prefer to use [] instead of array()
But you can also have
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
Integration with your example:
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
);
Try to read the documentation of the Class WP_Meta_Query at: https://codex.wordpress.org/Class_Reference/WP_Meta_Query#Initializing_WP_Meta_Query

Wordpress get posts with multiple meta values

I have a ACF select field which takes multiple value. Now I want to use get_posts() to get those custom posts. My arguments look like this:
$party = 'test1';
$function = 'test1, test2';
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'party',
'compare' => '=',
'value' => $party,
),
array(
'key' => 'function',
'compare' => 'IN',
'value' => array($function),
)
)
);
$items = get_posts($args);
But this does not work! Don't know what is wrong here!

Resources