最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Hide products in uncategorized category from search results

programmeradmin0浏览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' ); 

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' ); 
Share Improve this question edited May 15, 2019 at 13:11 netdev asked May 15, 2019 at 9:26 netdevnetdev 1052 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布评论

评论列表(0)

  1. 暂无评论