• Resolved clonica

    (@clonica)


    I want to remove all products from the wordpress search.

    I try to edit the products and set de ‘catalog visibility’ field to ‘hidden’ or ‘catalog’, but they continue to appear as search results.

    Why is it ignoring the visibility field? What can I do?

    Thanks

    • This topic was modified 7 years, 1 month ago by clonica.
Viewing 2 replies - 1 through 2 (of 2 total)
  • I have that problem too! Hopefully someone can help with this!

    Thread Starter clonica

    (@clonica)

    Finally I find a solution by adding this code in the functions.php of my active theme:

    add_action( 'pre_get_posts', 'jdn_modify_query' );
    function jdn_modify_query( $query ) {
     
    	// First, make sure this isn't the admin and is the main query, otherwise bail
    	if( is_admin() || ! $query->is_main_query() )
    	return;
    	 
    	// If this is a search result query
    	if( $query->is_search() ) {
    		// Gather all searchable post types
    		$in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );
    		// Define the post_type you want to exclude from the results
    		$post_type_to_remove = 'product';
    		// Make sure you got the proper results, and that your post type is in the results
    		if( is_array( $in_search_post_types ) && in_array( $post_type_to_remove, $in_search_post_types ) ) {
    			// Remove the post type from the array
    			unset( $in_search_post_types[ $post_type_to_remove ] );
    			// set the query to the remaining searchable post types
    			$query->set( 'post_type', $in_search_post_types );
    		}
    	}
    }

    This way you remove ALL the products from the search results.

    • This reply was modified 7 years, 1 month ago by clonica.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Unable to hide products from wordpress search’ is closed to new replies.