• Resolved losrack

    (@carlos-jaramillo)


    Hi,

    I used this code on my child’s functions.php to prevent a specific category to appear on the main shop.

    add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    
    function custom_pre_get_posts_query( $q ) {
    
    	if ( ! $q->is_main_query() ) return;
    	if ( ! $q->is_post_type_archive() ) return;
    
    	if ( ! is_admin() && is_shop() ) {
    
    		$q->set( 'tax_query', array(array(
    			'taxonomy' => 'product_cat',
    			'field' => 'slug',
    			'terms' => array( '110-vinilocuero' ), // Don't display products in the "110-vinilocuero" category on the shop page
    			'operator' => 'NOT IN'
    		)));
    
    	}
    
    	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    
    }

    But now I realized those products in this category are restricted form the search function.

    …So I was wondering if they can be included in the search.

    I am using storefront and woocommerce latest versions.

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Susan Langenes

    (@susanlangenes)

    Try adding

    if ( ! $q->is_search() ) return;

    after the other two lines that end with return;

    Thread Starter losrack

    (@carlos-jaramillo)

    Hi Susan,

    Thanks for the help but sadly it did not work …

    Any other ideas?

    regards…

    Thread Starter losrack

    (@carlos-jaramillo)

    If any body wonders the solution is …

    make this line …

    if ( ! is_admin() && is_shop() ) {

    into this …

    if ( ! is_admin() && is_shop() && ! is_search() )

    matter is settled.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘excluded category from main shop – search problem’ is closed to new replies.