Sorting Posts

You can sort the posts displayed in index pages like category or blog pages, and also those in Query Category pages. Themify themes include two WordPress filters to do this.

In the following code example, we're sorting the posts in Query Category pages alphabetically, that is, by post title name, and in ascending order so it goes from A to Z:

1. create a file named custom-functions.php in the theme root folder
2. open it and paste the following:

function custom_themify_query_posts_page_args($arg) {
 	$arg .= '&order=ASC&orderby=title';
 	return $arg; };
add_filter('themify_query_posts_page_args', 'custom_themify_query_posts_page_args');

3. save it (if you were editing it locally, upload it to your site by FTP) and check your site.

Another example would be to show posts in index or blog pages in random order. To do that:
1. use this code instead of the previous:

function custom_themify_query_posts_args($arg) {
 	$arg .= '&orderby=rand';
 	return $arg; };
 add_filter('themify_query_posts_args', 'custom_themify_query_posts_args'); 

2. go to theme settings > Default Layouts, enter all in the field labeled Query Categories and save.

Note in this second example that the filter used is different. For better control, Themify uses two filters to separate Query Category pages from index pages:

  • themify_query_posts_args: affects post sorting in index pages like category, tag or author archive pages.
  • themify_query_posts_page_args: affects post sorting in Query Category pages.

Here's a list of some of the parameters supported by each parameter, taken from WordPress documentation

  • order: Defaults to 'DESC'.
    • 'ASC' - ascending order from lowest to highest values (1, 2, 3; a, b, c).
    • 'DESC' - descending order from highest to lowest values (3, 2, 1; c, b, a).
  • orderby: Defaults to 'date'.
    • 'author' - Order by author.
    • 'title' - Order by title.
    • 'date' - Order by date.
    • 'modified' - Order by last modified date.
    • 'rand' - Random order.
    • 'comment_count' - Order by number of comments