Sort posts on whole site

Posted in: Newsmag
Post count: 108

Hi

I managed to add sort function to sort posts ASCENDING on frontpage. I need to set them for archives and category pages too.

Where can i do this?

Thank you

Post count: 6535

Hi

You can use the WordPress function is_category or similar to apply the code used for the homepage to the category page. Details about that function can be found here: https://codex.wordpress.org/Function_Reference/is_category

Thanks!

Post count: 108

Thank you .. i found some very good explaining articles about sorting, but i cannot see where to set this in template files? Archve.php and category.php files don’t have “have_posts” query …

Post count: 6535

Hi

The query that add the post in category and archive pages it’s made in loop.php template http://screencast.com/t/dtQtLmOe and you have to apply the modification to that file.

Thanks!

Post count: 108

Hi

Mean while i found a code, that i put into functions.php


// Runs before the posts are fetched
add_filter( 'pre_get_posts' , 'my_change_order' );
// Function accepting current query
function my_change_order( $query ) {
// Check if the query is for an archive
if($query->is_archive)
// Query was for archive, then set order
$query->set( 'order' , 'asc' );
// Return the query (else there's no more query, oops!)
return $query;
}

works like a charm 🙂

Thank you

Viewing 5 posts - 1 through 5 (of 5 total)
The forum ‘Newsmag’ is closed to new topics and replies.