Manual Ordering for Custom Post Types
Adds the Order field to chosen post types and sorts them by it on the front end and in the admin, so you can control the order of team members, services or FAQs.
Opens WPIDE on
Code
// Post types ordered by their Order field.
$wpide_ordered_types = array( 'team', 'service', 'faq' );
add_action( 'init', function () use ( $wpide_ordered_types ) {
foreach ( $wpide_ordered_types as $type ) {
if ( post_type_exists( $type ) ) {
add_post_type_support( $type, 'page-attributes' );
}
}
}, 99 );
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_ordered_types ) {
if ( ! $query->is_main_query() || $query->get( 'orderby' ) ) {
return;
}
$type = $query->get( 'post_type' );
if ( ( is_admin() && is_string( $type ) && in_array( $type, $wpide_ordered_types, true ) ) || ( ! is_admin() && $query->is_post_type_archive( $wpide_ordered_types ) ) ) {
$query->set( 'orderby', array( 'menu_order' => 'ASC', 'title' => 'ASC' ) );
}
} );
Notes
Set the order under Page Attributes (or "Order" in the block editor's sidebar). Items with the same number sort by title.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.