Loop through all featured products first & non featured products afterwards
-
I have written some code which displays all of the featured products alphabetically when the sorting option is set to “Sort by Featured”. I would like to be able to add a query onto the end of this so that instead of exclusively showing featured products, it would also have a loop to iterate and display non featured products afterwards. How could i make this work?
// Create sorting option add_filter( 'woocommerce_catalog_orderby', 'website_catalog_orderby' ); function website_catalog_orderby( $orderby ) { $orderby['featured'] = __('Sort by Featured', 'woocommerce'); return $orderby; } // Loop through all featured products add_action( 'woocommerce_product_query', 'website_product_query' ); function website_product_query( $q ) { if ( ! is_admin() && isset($_GET['orderby']) && 'featured' === esc_attr($_GET['orderby']) ) { $tax_query = $q->get('tax_query'); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', ); $q->set( 'tax_query', $tax_query ); $q->set( 'orderby', 'title' ); $q->set( 'order', 'ASC' ); // A-Z } }
Many Thanks.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Loop through all featured products first & non featured products afterwards’ is closed to new replies.