Skip Navigation

[Resolved] Order not working for Custom Post

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to change order of custom post type archive.

Solution:
When you are using WordPress standard archive, to change the archive post order, you need to use WordPress standard "pre_get_posts" action hook.

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/order-not-working-for-custom-post/#post-579789

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Author
Posts
#579750

I am trying to: I created a custom post type called Instructors (to list our Instructors) - I would like to see the Instructors on a page in Order by the Title field - which is actually their name.

Link to a page where the issue can be seen: hidden link
I have also just cloned the site here... to use for testing purposes. hidden link

I expected to see: the list in order by the title field. I am using the theme options for the display of this page but the Instructors only seem to appear in the order that I entered them in (with the last one showing up first and the first one I entered showing up last. I have changed the theme options to order by title, then tried by date, then tried by menu order(changing the menu on a few of them) = no changes in the order

Instead, I got:

#579789

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - as you are using WordPress standard archive to change the archive post order, you need to use "pre_get_posts" action.

I've added following code to your current theme's functions.php file:

add_action( 'pre_get_posts', 'my_change_sort_order'); 
function my_change_sort_order($query){
        if(is_post_type_archive('instructor')){
         //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
           //Set the order ASC or DESC
           $query->set( 'order', 'ASC' );
           //Set the orderby
           $query->set( 'orderby', 'title' );
		} 
}

Now, I can see the posts are order ASC and order by title.

#580002

Thank you that works perfectly!