Title: Query problems in category
Last modified: August 20, 2016

---

# Query problems in category

 *  [Wasim alhajebi](https://wordpress.org/support/users/wassimo/)
 * (@wassimo)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query-problems-in-category/)
 * Hi,
    i’m trying to sort posts by Views in categories but when i put this code
   in category.php , the posts are sorted by views but from diffrent categories,
   i just want to sort them by the current category
 *     ```
       <?php
       query_posts("meta_key=views&&orderby=meta_value_num&order=DES");
        while (have_posts()) : the_post();
        // the code
       endwhile;
       ?>
       ```
   
 * thanks

Viewing 1 replies (of 1 total)

 *  Thread Starter [Wasim alhajebi](https://wordpress.org/support/users/wassimo/)
 * (@wassimo)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/query-problems-in-category/#post-3085599)
 * Here is the correct code may someone of you need to use it
 *     ```
       <?php
       $args = array(
           'paged' => $paged,
           'meta_query'=> array(
               array()),
           'meta_key'=>'views',
           'orderby'=>'meta_value_num',
           'order'=>'DESC');
       $args = array_merge( $args , $wp_query->query );
       $posts = query_posts( $args );
       foreach( $posts as $post ) {
   
       // the code
       }
       ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Query problems in category’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Wasim alhajebi](https://wordpress.org/support/users/wassimo/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/query-problems-in-category/#post-3085599)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
