Title: Sort items in archive.php
Last modified: August 20, 2016

---

# Sort items in archive.php

 *  Resolved [briancostea](https://wordpress.org/support/users/briancostea/)
 * (@briancostea)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/)
 * I am trying to allow a user to sort the posts on the archive.php file by most
   commented or most recent. If I change the archive.php page to include this code
   based a querystring parameter, it doesn’t respect the month and year in the URL;
   it just displays all posts.
 * $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $args= array(‘
   orderby’ => ‘comment_count’, ‘order’ => ‘DESC’, ‘paged’ => $paged ); query_posts(
   $args);
 * Is there a way for me to grab the month and year out of the URL so that I can
   display just the posts for that month and year? If so, I can add it to the arguments
   in the $args array.
 * PS — I am using the out of the box archive widget to get to the site.com/year/
   month page (which displays the archive.php from the site theme).

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Thread Starter [briancostea](https://wordpress.org/support/users/briancostea/)
 * (@briancostea)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303369)
 * Bump
 *  Thread Starter [briancostea](https://wordpress.org/support/users/briancostea/)
 * (@briancostea)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303383)
 * Anyone out there?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303384)
 * > it doesn’t respect the month and year in the URL; it just displays all posts.
 * That’s because you have over-written the original query with your custom one.
   Try reviewing [http://codex.wordpress.org/Function_Reference/query_posts](http://codex.wordpress.org/Function_Reference/query_posts)
 *  Thread Starter [briancostea](https://wordpress.org/support/users/briancostea/)
 * (@briancostea)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303385)
 * Thank you for the response, esmi!
 * I understand that is the issue, but I am unsure as to how best retrieve the year
   and month from the URL. The only way for me to do a custom sort on the page is
   to use a custom query :).
 * I have reviewed the query_posts doc but it doesn’t contain any info as to how
   to sort specifically inside of the archive.php or category.php files.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303390)
 * > The only way for me to do a custom sort on the page is to use a custom query
 * That’s one way to do it, yes. But instead of replacing the standard query, have
   you looked at concatenating your custom order & orderby parameters onto it? See
   Example 3 on that Codex page.
 *  Thread Starter [briancostea](https://wordpress.org/support/users/briancostea/)
 * (@briancostea)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303392)
 * I will try that and let you know. And thanks!
 *  Thread Starter [briancostea](https://wordpress.org/support/users/briancostea/)
 * (@briancostea)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303417)
 * Sorry for the late response.
 * Thanks, esmi! Worked great. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Sort items in archive.php’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [briancostea](https://wordpress.org/support/users/briancostea/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/sort-items-in-archivephp/#post-2303417)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
