0

I have about 20000 products and I am using electro theme. All of them belong to uncategorized category (id: 979) and most of them belong to other categories also. I want to hide from the search result the ones that only belong to uncategorized. I also want to hide the uncategorized category from the product page.

I used this code and it hides the uncategorized category from shop page but products are still showing in search results:

function wc_hide_selected_terms( $terms, $taxonomies, $args ) {
    $new_terms = array();
    if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
        foreach ( $terms as $key => $term ) {
              if ( ! in_array( $term->slug, array( 'uncategorized' ) ) ) {
                $new_terms[] = $term;
              }
        }
        $terms = $new_terms;
    }
    return $terms;
}
add_filter( 'get_terms', 'wc_hide_selected_terms', 10, 3 );

These are attempts to hide the uncategorized (only) products from search results with no luck:

function my_electro_search_categories_filter_args($args) {
    $args['exclude'] = 'uncategorized';
    return $args;

}
add_filter('electro_search_categories_filter_args', 'my_electro_search_categories_filter_args');

function sp_pre_get_posts( $query ) {
    if ( !is_admin() && $query->is_main_query())  {
        $query->set( 'cat', '-979' );
    }
}
add_action( 'pre_get_posts', 'sp_pre_get_posts' );

UPDATE

I fixed it with the help of the answer below like this (but I dont know if its ok in performance matter):

function custom_pre_get_posts_query( $q ) {

$tax_query = (array) $q->get( 'tax_query' );

$tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array( 'axesouar-kinitis', 'gnisia-axesouar', 'axesouar-tablet-hy', 'tilefonia', 'statheri-tilefonia', 'lipa-prionta', 'exoplismos-service-norton', 'antallaktika' ),
    'operator' => 'IN'
    //'terms' => array( 'uncategorized' ),
    //'operator' => 'NOT IN'
);


$q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' ); 
0

Try to add the following lines of code at the end of your theme’s functions.php file:

function custom_pre_get_posts_query( $q ) {

$tax_query = (array) $q->get( 'tax_query' );

$tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array( 'uncategorized' ),
    'operator' => 'NOT IN'
);


$q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

Reference Site

  • It works but it also removes the products that belong to other categories also. I want to remove only the products that belong to uncategorized only – netdev May 15 at 10:04
  • I updated the function please try to check the updated code. – Nitish Paswan May 15 at 10:06
  • 'operator' => 'NOT IN' excludes all posts that are in the uncategorized category, even if they are also in other categories. – nmr May 15 at 10:10
  • same.... I guess the problem in the code is that it gets the products that dont belong to uncategorized category. But I want to display products that belong to uncategorized and other categories also. I want to hide only the products that belong ONLY one category - the uncategorized – netdev May 15 at 10:10
  • Maybe it needs something more complex like : if it belongs to uncategorized and another one then display it OR if NOT IN uncategorized – netdev May 15 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.