WoocommerceWordpressWordpress Tricks

WordPress query to get Woocommerce Products order by sales, pricing and rating

149Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, WordPress query to get Woocommerce Products order by sales, pricing and rating.

If you are new in WordPress and Woocommerce then you can check my old posts related to WordPress and Woocommerce.


Wordpress query to get Woocommerce Products order by sales, pricing and rating

I was asked by someone to make custom query like Woocommerce default shop page orderby rating, price and popularity.

And I am going to share this code and please use it carefully and you can use this your WordPress theme’s template file:

1. I have used this by form post and you can try this your own way:

<?php switch ($_POST['categoryText']){
                case 'price':
                     $args = array(
                        'post_type'      => 'product',
                        'orderby'        => 'meta_value_num',
                        'order'          => 'asc',
                        'meta_key'       => '_price'
                        );
                    break;
                case 'price-desc':
                    $args = array(
                        'post_type'      => 'product',
                        'orderby'        => 'meta_value_num',
                        'order'          => 'desc',
                        'meta_key'       => '_price'
                        );
                    break;
                case 'rating':
                     $args = array(
                        'post_type'      => 'product',
                        'orderby'        => 'meta_value_num',
                        'order'          => 'desc',
                        'meta_key'       => '_wc_average_rating'
                        );
                    break;
                case 'popularity':
                    $args = array(
                        'post_type'      => 'product',
                        'orderby'        => 'meta_value_num',
                        'order'          => 'desc',
                        'meta_key'       => 'total_sales'
                        );
                    break;
                case 'date':
                    $args = array(
                        'post_type'      => 'product',
                        'order'          => 'desc',
                        );
                    break;
            }
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
        if(has_post_thumbnail( get_the_ID() )) { 
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ));
        ?>
        <img src="<?php echo $image[0]; ?>">
        <?php } else { ?>
        <img src="<?php echo plugins_url().'/woocommerce/assets/images/placeholder.png';?>">
        <?php } ?>
        <a href="<?php echo the_permalink(get_the_ID());?>" title="<?php echo get_the_title();?>"><?php echo get_the_title();?></a>
        <?php endwhile;
    }
    wp_reset_postdata();

 

 

If you have any query related to this post then please let me know.

Jassa Jatt,

Thank you

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL