Categories

Monday, December 23, 2024
#919814419350 therichposts@gmail.com
WoocommerceWordpressWordpress Tricks

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

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.