To sort Word Press posts by post modified date and time. You may need this to show posts that have recently updated or some other reason to sort this way. Word Press provide query_posts function to change order of blog posts in several ways. This function support lot of parameter to customize posts listing. First, i am writing about parameter required to sort post and then talk about other parameter available to use with query_posts function.
How to Use
- Open required theme files using ftp(recommended) or theme editor in which you want to sort posts list by modified date. For example: index.php for home page posts listing, category.php for category posts listing and archive.php for archive page listing. Do the same for any other customized post listing pages.
- Add query_posts function just above loop with orderby=modified parameter. If query_posts parameter is already there just append orderby=modified parameter at the end of parameter list. see example below.
global $query_string; $posts=query_posts($query_string . '&orderby=modified'); if (have_posts()) : while (have_posts()) : the_post();
- This code will not work for next pages 2,3,4… because we are not passing page number here. You don’t need to pass page number if you are only showing limited posts in sidebar or some other special area. To fix this issue for normal posts listing simply grab page number value and pass it to function. Check code below.
global $query_string; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts($query_string . '&orderby=modified&paged='.$paged);
- You may also use order=ASC or order=DESC to sort posts in ascending or descending order.
- orderby accept following values if you want to order by some other fields. values are: id, author, title, date, parent, rand, comment_count, menu_order, meta_value, meta_value_num
Other Customization
You can customize according to your need with several other parameter available. This section will list down some of useful parameters.
- posts_per_page – accept integer to show number of posts per page.
- tag – accept tag name as string to show posts lists from certain tag.
- order – accept ASC or DESC parameter to sort posts in ascending or descending order.
- cat – accept integer with minus or without minus sign. without minus sign represent plus which mean include posts from specified categories and minus sign means exclude posts from specified categories. You may use multiple categories with comma.
- category_name – Useful if you want to use category name instead of category number.
- year – accept numeric 4 digit year.
- monthnum – accept numeric month number. You may use year parameter along-with monthnum to show posts from certain month in a year.
- category__and – accept more then one category number in array and include posts that associated with all specified categories.
- category__in – accept more then one category number in array and include posts from either categories. This does not include posts from subcategories.
- category__not_in – accept more then one category number in array and exclude from either categories.
- post_type – accept post type like page, post, attachment or custom taxonomy.
- post__in – accept more then one post number in array and include only those posts.
- author – accept numeric integer parameter. Use multiple author ids with comma to include posts from multiple authors. minus sign can be used to exclude certain author posts.
- author_name – accept string parameter as author name.
- post_status – retrieves posts by post status. possible values are publish,pending,draft,auto-draft,future,private,inherit,trash. default value is publish.