• Resolved pettrushkov

    (@pettrushkov)


    Hello, I have query with arguments for products, but I want to display featured products first and other products by date after them.

    P.S. I can’t do this by 2 different queries, because on this pages there is ajax load more (with pagination), so I should have all posts in 1 query

      $cate = get_queried_object();
                $cateID = $cate->term_id ?? null;
    
                $args = array(
                    'post_type' => 'product',
                    'post_status' => 'publish',
                    'orderby' => 'date', // ... order by date
                    'order' => 'DESC', // ... most recent first
                    'meta_query' => array(
                        array(
                            'key' => '_price',
                            'value' => 0,
                            'compare' => '>',
                            'type' => 'NUMERIC'
                        )
                    )
                );
    
                if($cateID) {
                    $args['tax_query'] = array(
                        array(
                            'taxonomy' => 'product_cat',
                            'field' => 'id',
                            'terms' => $cateID,
                        )
                    );
                }
    
                $query = new WP_Query( $args );
                while ($query->have_posts()) {
                    $query->the_post();
    
                    /**
                     * Hook: woocommerce_shop_loop.
                     */
                    do_action('woocommerce_shop_loop');
    
                    wc_get_template_part('content', 'product');
                }
                wp_reset_postdata();
Viewing 3 replies - 1 through 3 (of 3 total)
  • Saif

    (@babylon1999)

    Hello @pettrushkov,

    Thank you for reaching out!

    Can you please provide more details on what you’re trying to achieve?

    If this is for a custom page, then it’s much easier to achieve the same result with the [products] shortcode.

    You can add it twice to the same page, one with and without the featured attribute.

    To learn more about WooCommerce shortcodes, please check this guide: https://woocommerce.com/document/woocommerce-shortcodes/#available-product-attributes

    Let us know how it goes!

    Thread Starter pettrushkov

    (@pettrushkov)

    I found solution for my problem (orderby featured product and after them by date)

    Use “featured_products” as ‘orderby’ value

    // Add custom orderby by featured products and other by date - 'featured_products'
    function featured_products_orderby( $orderby, $query ) {
        global $wpdb;
    
        if ( 'featured_products' == $query->get( 'orderby' ) ) {
            $featured_product_ids = (array) wc_get_featured_product_ids();
            if ( count( $featured_product_ids ) ) {
                $string_of_ids = '(' . implode( ',', $featured_product_ids ) . ')';
                $orderby = "( {$wpdb->posts}.ID IN {$string_of_ids}) " . $query->get( 'order' )." , post_date DESC";
            }
        }
    
        return $orderby;
    }
    add_filter( 'posts_orderby', 'featured_products_orderby', 10, 2 );
    Plugin Support Shameem a11n

    (@shameemreza)

    Hi @pettrushkov

    I’m glad you were able to find a solution to your inquiry here and thanks for sharing it with the community too! ??

    I will be marking this thread as resolved. Should you have further inquiries, kindly create a new topic here.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Featured products should be at top of posts list’ is closed to new replies.