0

On my shop page I want to display only a few select items. I figured the simplest way to do this would be to mark those that I want to show as 'featured' - via the product edit screen in my Wordpress dashboard.

I found this following code which I thought worked. However, I discovered it works only if I am logged in as Admin.

Is there a way to show the 'featured' products ONLY on the shop page?

// Display featured products in shop pages
add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
function custom_product_query_tax_query( $tax_query, $query ) {
    if( is_admin() ) return $tax_query;

    if ( is_shop() ) {
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured'
        );
    }

    return $tax_query;
}
Braiam
  • 1
  • 11
  • 47
  • 78
AAA
  • 13
  • 4
  • _"I discovered it works only if I am logged in as Admin"_ - This is certainly not the case for a default WooCommerce setup, [is_admin()](https://developer.wordpress.org/reference/functions/is_admin/) determines whether the current request is for an administrative interface page. It has nothing to do with the admin user role – 7uc1f3r Apr 04 '22 at 14:35
  • @7uc1f3r yeah I know, I did think that – AAA Apr 04 '22 at 14:56
  • I found this, and it works. `add_action( 'pre_get_posts', 'bbloomer_remove_products_from_shop_page' ); function bbloomer_remove_products_from_shop_page( $q ) { if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return; if ( ! is_admin() && is_shop() ) { $q->set( 'post__in', array(0) ); } remove_action( 'pre_get_posts', 'bbloomer_remove_products_from_shop_page' ); }` – AAA Apr 04 '22 at 14:57

0 Answers0