Woocommerce product search in admin is not working - woocommerce

I have a list of products in woocommerce
when I search for a product (in admin), it always says "No Products found":
This is the URL /wp-admin/edit.php?s=Deluxe&post_status=all&post_type=product&action=-1&m=0&product_cat&product_type&paged=1&action2=-1
and when I remove product_cat from the URL the product comes up

Add in the following snippet to function.php:
add_action( 'pre_get_posts', 'products_pre_get_posts' );
function products_pre_get_posts( $query ) {
if(is_admin()){
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
)
));
}
}

I had the same problem. After disable the YoastSeo plugin, the search works properly again.

The Answer from Chandrakant Devani helped, but broke other searches in the admin. Adding an if seems to avoid breakages
if ( is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product')
full code:
add_action( 'pre_get_posts', 'products_pre_get_posts' );
function products_pre_get_posts( WP_Query $query ) {
if ( is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product' ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
)
) );
}
}

Related

Woocommerce woocommerce_product_query hook is not working

I need to hide a few products from the shop page. And I'm doing it by using a taxonomy called subscription_tier but following code doesn't work.
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'subscription_tiers',
'field' => 'slug',
'terms' => array( 'standard' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
I even tried changing 'taxonomy' => 'product_cat', and 'terms' => array( 'women' ). But this wont affect my results

Exclude products from a certain category, with Woocommerce and Elementor Anywhere

I am using Elementor Anywhere to create a specific display for my WooCommerce products.
I have a filter and for the filter works I need to use Post blocks Adv.
There I ask to display current archive.
I have the possibility to put a query filter, but I can't exclude products from a certain category.
I have this, but it doesn't work with current archives.
function filtrer_produit_sans_crea( $query_args ) {
$query_args['meta_query'] = array(
array(
'key' => '_stock_status',
'value' => 'instock',
),
);
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'coin-des-creatrices',
'operator' => 'NOT IN',
);
return $query_args;
}
add_filter( 'filtre_zero_dechet', 'filtrer_produit_sans_crea' );
Has anyone ever tried something like this?
Or do you have a clue?
OK I found a solution:
function mystore_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );
But it does on the whole site.
If anyone has a clue how to do it only on a specific page...
Naively I wanted to add this:
function mystore_pre_get_posts_query( $q ) {
if ( is_page( 54 ) ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );
But it's doesn't work ^_^
I found another track.
function mystore_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'wp_head', 'remove_my_action');
function remove_my_action($post) {
global $post;
$post_id = $post->ID;
if (160029 == $post_id) {
echo "PAGE OK";
add_action( 'pre_get_posts', 'mystore_pre_get_posts_query');
}else{
remove_action('pre_get_posts', 'mystore_pre_get_posts_query');
}
}
My "page ok" displays well but the function does not launch.
If it is outside this function, the line add_action('pre_get_posts', 'mystore_pre_get_posts_query');
does what I want it to do.
Is it add_action( 'wp_head', 'remove_my_action'); that is not the right one?

Exclude posts that belong to a specific custom taxonomy term even if it belong to another taxonomy term

I would like to exclude posts that belong to a specific custom taxonomy term "for example id=37" (and childs) even if it belong to another(s) taxonomy term.
Here is my function :
add_action( 'pre_get_posts', 'films_query' );
function films_query( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
$taxquery = array(
array(
'taxonomy' => 'categorie_films',
'field' => 'term_id',
'terms' => 37,
'operator' => 'NOT IN',
'include_children' => true,
)
);
$query->set( 'posts_per_page', '-1' );
$query->set( 'post_status', 'publish' );
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
Any help appreciated.
Best.
Pierre

Exclude products with product shortocde Woocommerce

I am trying to exclude products with shortcode but its not working
here is what i am using shortocode
[products limit="14" orderby="id" columns="4" order="DESC" visibility="visible" category="tcs" cat_operator="NOT IN"]
I used php filter also
function phl_customize_product_shortcode( $args, $atts ) {
if ( is_front_page() ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => array( 'tcs' ),
'operator' => 'NOT IN'
)
);
}
return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'phl_customize_product_shortcode', 10, 2 );
But still not working
here is site you can see
https://www.pottershouselimited.co.uk
can you try this:
function phl_customize_product_shortcode( $args, $atts ) {
if ( is_front_page() ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'tcs' ),
'operator' => 'NOT IN'
)
);
}
return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'phl_customize_product_shortcode', 10, 2 );
and tell me how it goes? also where on the website can i see the tcs items on homepage?

Exclude category from search results [duplicate]

I'm using this code to exclude some post category from the wordpress search results :
function SearchFilter($query)
{
if ($query->is_search)
{
$query->set('cat', '-709,-710,-614');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
My problem is that it doesn't work for woocommerce categories and products are not filtered.
How can I filter some woocommerce categories too ?
Can you replace your category. hope this help you.
function wpse188669_pre_get_posts( $query ) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_search()
) {
$query->set( 'post_type', array( 'product' ) );
// set your parameters according to
// https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
$tax_query = array(
array(
// likely what you are after
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-2',
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );

Resources