Title: Sort posts by updatetime
Last modified: August 21, 2016

---

# Sort posts by updatetime

 *  [erikture](https://wordpress.org/support/users/erikture/)
 * (@erikture)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/sort-posts-by-updatetime/)
 * Is it possible to change the sortorder of posts of a category.
    By default they
   are sorted by the date when they were first posted. I would like to sort them
   automaticaly by the time when they were last updated, with the most resently 
   updated post first.

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

 *  [acub](https://wordpress.org/support/users/acub/)
 * (@acub)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/sort-posts-by-updatetime/#post-4520769)
 * Yes, it’s possible. There are two requirements:
    1. You need a child theme. 2.
   You need to know your category ID or the category slug. To find out the category
   ID go to your categories and hover over the edit category link. Watch the href
   displayed for the value of tag_ID.
 * Now, add this code in functions.php of your child theme and replace {$cat_ID_or_slug}
   with either category ID or the category slug:
 *     ```
       function sort_category( $query ) {
       	if (	$query->is_category('{$cat_ID_or_slug}')
       	&&	$query->is_main_query()
       	&&	! $query->is_admin() ) {
       		$query->set( 'orderby', 'modified' );
           		}
       	}
   
       add_action( 'pre_get_posts', 'sort_category' );
       ```
   
 * I can’t test it right now. Get back to me if it doesn’t work. It should work,
   afaik is_category() is inside query.php, so it’s usable with pre_get_posts.
 *  [acub](https://wordpress.org/support/users/acub/)
 * (@acub)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/sort-posts-by-updatetime/#post-4520832)
 * I’ve tested the code above and is_category() conditional doesn’t seem to work
   during pre_get_posts. Only $query->is_category works, if you’re on any category
   page. I tried a few more methods and this one seems to work:
 *     ```
       add_action('wp_head', 'sort_category');
   
       function sort_category() {
       	global $query_string;
       	if (	is_category('category-slug')
       	&&	is_main_query()
       	&& ! 	is_admin() )
       	$posts = query_posts( $query_string . '&orderby=modified' );
       	}
       ```
   
 *  [acub](https://wordpress.org/support/users/acub/)
 * (@acub)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/sort-posts-by-updatetime/#post-4520836)
 * Note: Change `category-slug` above with the actual category slug.
 * Another note: The first code is supposed to work, too, I don’t know why it didn’t
   in my tests. It might have to do with a bug on using is_category(tag_ID) on $
   wp_query, according to comments in [this thread](http://wordpress.stackexchange.com/questions/125041/is-category-in-pre-get-posts-strange-error).
 * So basically it needs a check on whether the category with that id actually exists:
 *     ```
       function sort_category( $query ) {
       	if (	$query->is_main_query()
       	&&	term_exists(314,'category')
       	&&	$query->is_category(314)
       	&& ! 	is_admin() ) {
       		$query->set( 'orderby', 'modified' );
           		}
       	}
       add_action( 'pre_get_posts', 'sort_category' );
       ```
   
 * where 314 needs to be replaced with the actual category ID.

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

The topic ‘Sort posts by updatetime’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/customizr/4.4.24/screenshot.png)
 * Customizr
 * [Support Threads](https://wordpress.org/support/theme/customizr/)
 * [Active Topics](https://wordpress.org/support/theme/customizr/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/customizr/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/customizr/reviews/)

## Tags

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

 * 3 replies
 * 2 participants
 * Last reply from: [acub](https://wordpress.org/support/users/acub/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/sort-posts-by-updatetime/#post-4520836)
 * Status: not resolved