Title: sort posts (sort by views) using post_views_count
Last modified: August 20, 2016

---

# sort posts (sort by views) using post_views_count

 *  [swadeshib](https://wordpress.org/support/users/swadeshib/)
 * (@swadeshib)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/sort-posts-sort-by-views-using-post_views_count/)
 * i want to sort posts (sort by views) using this function
 * i am currently using this code in function.php to get post views count
 *     ```
       <?php
       function getPostViews($postID){
           $count_key = 'post_views_count';
           $count = get_post_meta($postID, $count_key, true);
           if($count==''){
               delete_post_meta($postID, $count_key);
               add_post_meta($postID, $count_key, '0');
               return "0 View";
           }
           return $count.' Views';
       }
       function setPostViews($postID) {
           $count_key = 'post_views_count';
           $count = get_post_meta($postID, $count_key, true);
           if($count==''){
               $count = 0;
               delete_post_meta($postID, $count_key);
               add_post_meta($postID, $count_key, '0');
           }else{
               $count++;
               update_post_meta($postID, $count_key, $count);
           }
       }
       ?>
       ```
   
 * i want to integrate post_views_count to sort posts with this code
 *     ```
       $params = $_GET;
       unset($params['orderby']);
       unset($params['order']);
       unset($params['v_orderby']);
       unset($params['v_sortby']);
       $url = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
       $url = strtok($url, '?');
       $url =$url.'?'.http_build_query($params);
       ```
   
 *     ```
       &orderby=comment_count&order=desc
       &v_sortby=views&v_orderby=desc
       ```
   
 *     ```
       $active = "date";
       if ($_GET["orderby"]=="comment_count"){
           $active = "comment";
       }
       if ($_GET["v_sortby"]=="views"){
           $active = "views";
       }
       ```
   

The topic ‘sort posts (sort by views) using post_views_count’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [swadeshib](https://wordpress.org/support/users/swadeshib/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/sort-posts-sort-by-views-using-post_views_count/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
