Plugin Author
Ajay
(@ajay)
I’m not as close to woocommerce. How do you determine which related product is out of stock. I mean where is this stored exactly?
The code would require a bit of work.
function yarpp_custom_wp_query($query) {
if(isset($query->yarpp_cache_type)){
$query->set('post_type', 'product');
$meta_query = [
'relation' => 'AND',
[
'key' => '_stock_status',
'value' => ['instock','onbackorder'],
'compare' => 'IN',
]
];
$query->set('meta_query', $meta_query);
}
return $query;
}
add_filter('pre_get_posts', 'yarpp_custom_wp_query', 100);
I determine if it’s in stock or not when I create or edit a product, it’s the same as a post but it’s a woocommerce product.
maybe this code above, from another related plugin can help
Plugin Author
Ajay
(@ajay)
Can you please try this and let me know if this works?
function crp_custom_wp_query($query) {
if(is_singular('product') && $query->get( 'crp_query'){
$query->set('post_type', 'product');
$meta_query = [
'relation' => 'AND',
[
'key' => '_stock_status',
'value' => ['instock','onbackorder'],
'compare' => 'IN',
]
];
$query->set('meta_query', $meta_query);
}
return $query;
}
add_filter('pre_get_posts', 'crp_custom_wp_query', 100);
This will check if this is a product view and if that is the case it should limit the products
hello, yes, it totally worked, I just had to add a “)” in the second line of code, it was like this:
function crp_custom_wp_query($query) {
if(is_singular('product') && $query->get( 'crp_query')){
$query->set('post_type', 'product');
$meta_query = [
'relation' => 'AND',
[
'key' => '_stock_status',
'value' => ['instock','onbackorder'],
'compare' => 'IN',
]
];
$query->set('meta_query', $meta_query);
}
return $query;
}
add_filter('pre_get_posts', 'crp_custom_wp_query', 100);
Thank you very much 😀
Plugin Author
Ajay
(@ajay)
You’re welcome! Sorry I missed that bracket. Always hard to see it here in the forum.
Do consider a review of the plugin.
https://wordpress.org/support/plugin/contextual-related-posts/reviews/