Home / Admin / Sort Events Category by Publish Date (Oldest First)
Duplicate Snippet

Embed Snippet on Your Site

Sort Events Category by Publish Date (Oldest First)

Code Preview
php
<?php
function sort_posts_by_category($query) {
    if ($query->is_main_query() && !is_admin()) {
        $oldest_first_categories = array(1483,1486,1485,1484,1482,1391); // Replace with the IDs of the categories where you want to display oldest posts first
        if (is_category($oldest_first_categories)) {
         //   $query->set('order', 'ASC');
        //    $query->set('orderby', 'date');
			$query->set( 'meta_key', 'start-date' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
        } else {
            $query->set('order', 'DESC');
            $query->set('orderby', 'date');
        }
    }
    return $query;
}
add_filter('pre_get_posts', 'sort_posts_by_category');

Comments

Add a Comment