ascending order custom post type

You don’t declare the order of the results when you register the post type.

Instead, you do at the time the results are requested. If you’re using WP_Query to get the results then you add your orderby and order arguments to the request.

http://codex.wordpress.org/Class_Reference/WP_Query

If you want to change the order of the results on the archive pages such as http://www.yoursite.com/calendar/ then you have to filter the existing query by can using the pre_get_posts filter like so:

function wpse_167441_reorder_calendar($query) {
  if ( !is_admin() && $query->is_main_query() ) {

    if (is_post_type_archive('calendar')) {
      $query->set('orderby', 'date' );
      $query->set('order', 'ASC' );
    }

  }
}

add_action('pre_get_posts','wpse_167441_reorder_calendar');

If you want this to be the case in the admin area too then you can remove the !is_admin check. Be sure to test that you have not effected other post types when you’re doing this – that’s why the is_post_type_archive condition is in there.

http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Hope that’s what you were after.

Leave a Comment

tech