Title: Sort carousel using $args[&#039;orderby&#039;]
Last modified: August 21, 2016

---

# Sort carousel using $args['orderby']

 *  [Dani1091](https://wordpress.org/support/users/dani1091/)
 * (@dani1091)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/sort-carousel-using-argsorderby/)
 * Hello friends,
 * I need your help with this: I want to sort my carousel of popular coupons as 
   you can see [here](http://idechpanama.com/cuponiacos/) (the second one) sorting
   it from comment_count to gdsr_sort with thumbs (using old plugin GD Star rating).
 * Hoping someone can guide me. I’m a beginner on php. Regards.
 * Here’s the code who calls the popular coupons carousel to the homepage:
 *     ```
       <div class="row collapse">
               <div class="column">
                   <div class="items-carousel flexslider">
                       <ul class="rr slides">
                           <?php
                           $args = array();
                           $args['posts_per_page'] = 20;
                           $args['post_type'] = 'coupons';
                           $args['orderby'] = 'comment_count';
                           $args['order'] = 'DESC';
                           $query = new WP_Query($args);
                           while($query->have_posts() ) : $query->the_post();
                               $discount = get_post_meta($post->ID, '_single_discount', true);
                               $date = get_post_meta($post->ID, '_single_date', true);
                               $code = get_post_meta($post->ID, '_single_code', true);
                               $url = get_post_meta($post->ID, '_single_url', true);
                               $thumbs = array();
                               $categories = get_the_category();
                               $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                               if($categories) {
                                   foreach($categories as $category) {
                                       $image = get_option('taxonomy_' . $category->term_id);
                                       if($image) {
                                            if($thumb == '') {
                                               $thumb = $image['custom_term_meta'];
                                           }
                                           $category_id = $category->term_id;
                                       }
                                   }
                               }
                               $daysleft = round( ($date-time()) / 24 / 60 / 60);
                               ?>
       ```
   
 * And here is the code of functions.php who defines the ‘gdsr_sort’ sorting of 
   posts.
 *     ```
       //modify-ing the custom query on category pages
   
       add_action( 'parse_query','changept' );
       function changept() {
       	if( (is_category() || is_author() ) && !is_admin() ) {
       		set_query_var( 'post_type', array( 'post', 'coupons' ) );
       	}
       	if( (is_category() || is_search() || is_author() || is_tag() ) && !is_admin() ) {
       		if(isset($_GET['sort']) && $_GET['sort'] == 'popular') {
                 	set_query_var('orderby', 'comment_count');
                 	set_query_var('order', 'desc');
               }
               elseif(isset($_GET['sort']) && $_GET['sort'] == 'thumbs') {
                   set_query_var('meta_query', array(
                       array(
                            	'key' => '_single_date',
                            	'value' =>  strtotime("now"),
                            	'compare' => '>=',
                            	'type' => 'UNSIGNED'
                        	)
                   ) );
                   set_query_var('gdsr_sort', 'thumbs');
                   set_query_var('sort_order', 'desc');
               }
       ```
   

The topic ‘Sort carousel using $args['orderby']’ is closed to new replies.

## Tags

 * [orderby](https://wordpress.org/support/topic-tag/orderby/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [Dani1091](https://wordpress.org/support/users/dani1091/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/sort-carousel-using-argsorderby/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
