Skip to main content
added 46 characters in body
Source Link
Krzysiek Dróżdż
  • 25.5k
  • 9
  • 53
  • 74

I'm trying to hide my Woocommerce products that are out of stock from search, while still showing other post types in the results. The post type for the products are product.

add_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );

function hide_out_of_stock_from_search( $q ) {
    
    if ( ! $q->is_search() ) return;
    if ( is_post_type_archive( 'product' ) ) {

        $q->set( 'meta_query', array(array(
            'key'       => '_stock_status',
            'value'     => 'outofstock',
            'compare'   => 'NOT IN'
        )));

    }

    remove_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );
 
}

Everything is working except is_post_type_archive( 'product' ), which doesn't return true. If I replace it with true, it hide everything from search query (even posts and pages) except products that are in stock.

How can I set meta_query only for a specific post type without affecting other post types in the search query?

I'm trying to hide my Woocommerce products that are out of stock from search, while still showing other post types in the results. The post type for the products are product.

add_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );

function hide_out_of_stock_from_search( $q ) {
    
if ( ! $q->is_search() ) return;
if ( is_post_type_archive( 'product' ) ) {

$q->set( 'meta_query', array(array(
    'key'       => '_stock_status',
    'value'     => 'outofstock',
    'compare'   => 'NOT IN'
)));

}

remove_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );
 
}

Everything is working except is_post_type_archive( 'product' ), which doesn't return true. If I replace it with true, it hide everything from search query (even posts and pages) except products that are in stock.

How can I set meta_query only for a specific post type without affecting other post types in the search query?

I'm trying to hide my Woocommerce products that are out of stock from search, while still showing other post types in the results. The post type for the products are product.

add_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );

function hide_out_of_stock_from_search( $q ) {
    
    if ( ! $q->is_search() ) return;
    if ( is_post_type_archive( 'product' ) ) {

        $q->set( 'meta_query', array(array(
            'key'       => '_stock_status',
            'value'     => 'outofstock',
            'compare'   => 'NOT IN'
        )));

    }

    remove_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );
}

Everything is working except is_post_type_archive( 'product' ), which doesn't return true. If I replace it with true, it hide everything from search query (even posts and pages) except products that are in stock.

How can I set meta_query only for a specific post type without affecting other post types in the search query?

Source Link

Set meta_query only for specific post type

I'm trying to hide my Woocommerce products that are out of stock from search, while still showing other post types in the results. The post type for the products are product.

add_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );

function hide_out_of_stock_from_search( $q ) {
    
if ( ! $q->is_search() ) return;
if ( is_post_type_archive( 'product' ) ) {

$q->set( 'meta_query', array(array(
    'key'       => '_stock_status',
    'value'     => 'outofstock',
    'compare'   => 'NOT IN'
)));

}

remove_action( 'pre_get_posts', 'hide_out_of_stock_from_search' );

}

Everything is working except is_post_type_archive( 'product' ), which doesn't return true. If I replace it with true, it hide everything from search query (even posts and pages) except products that are in stock.

How can I set meta_query only for a specific post type without affecting other post types in the search query?