In WooCommerce, you can use the pre_get_posts action to modify the query that retrieves products for the shop page and exclude products that are protected.

Here is an example of how you can use the pre_get_posts action to hide all protected products from the shop page:

Copy codeadd_action( 'pre_get_posts', 'hide_protected_products_shop' );
function hide_protected_products_shop( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_shop() ) {
        $query->set( 'meta_query', array(
            array(
                'key' => '_visibility',
                'value' => 'hidden',
                'compare' => 'NOT LIKE'
            )
        ) );
    }
}

This code uses the pre_get_posts action to modify the query that retrieves products for the shop page, and it adds a meta query that excludes products that have a visibility of “hidden” by checking the custom field _visibility.

The is_shop() function is used to target only the shop page, and the is_main_query() function is used to target only the main query and not any custom queries.

You can also use the tax_query to exclude the protected products.

Copy codeadd_action( 'pre_get_posts', 'hide_protected_products_shop' );
function hide_protected_products_shop( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_shop() ) {
        $query->set( 'tax_query', array(
            array
(Visited 6 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window