• I’m trying to query for best selling products (popular) and featured products using one loop. And, potentially being able to add an identifier to each product –as a class– to be able to filter them.

    I’ve been able to query for the featured products like so:

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'meta_key' => '_featured',
        'meta_value' => 'yes'
    );

    And for the Popular Products, this:

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num'
    );

    Using this loop:

    $loop = new WP_Query( $args );
    
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post();
    
            // stuff
    
        endwhile;
    else :
    
        // Nothing
    
    endif;
    wp_reset_postdata();

    I’ve already tried some stuff to join them together with no luck, like:

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'meta_key' => '_featured',
                'meta_value' => 'yes'
            ),
            array(
                'meta_key' => 'total_sales',
                'orderby' => 'meta_value_num'
            )
        )
    );

    Can’t really understand what’s wrong…. Any help greatly appreciated.

    https://wordpress.org/plugins/woocommerce/

  • The topic ‘How to query Popular Products and Featured products in same loop’ is closed to new replies.