Applying the customized order when using the Post Types Order plugin
Last updated on: December 1, 2025 2:31 am
- If you enable Auto Apply Sort in the plugin, you don’t need to change any code — the plugin will override compatible queries and apply the saved order automatically.
- The Post Types Order plugin stores a custom sequence using WordPress’ built-in menu_order field. To retrieve posts in that order inside your templates or code, use a WP query with orderby => ‘menu_order’ (and order => ‘ASC’ or ‘DESC’).
- Use suppress_filters => false or the main-query hook (pre_get_posts) if your custom queries ignore filters. If you only want the order applied on a specific page (home/archive/custom), use pre_get_posts conditionals.
Table of contents
- Quick example
- Where to put the code
- Apply ordering only to selected queries (recommended)
- Using Post Types Order with the REST API
- Programmatically setting a menu order
- Troubleshooting & caching
- Advanced: ignore AutoSort for specific queries
- Performance notes & best practices
- Full example snippets
1) Quick example
If you already have a custom loop and want the posts in the order you arranged in the plugin:
$args = array(
'post_type' => 'feature', // or 'post', or your CPT
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
// your template code
}
wp_reset_postdata();
}
Notes:
- orderby => ‘menu_order’ will return posts ordered by the value managed by Post Types Order.
- Use ‘order’ => ‘DESC’ to reverse.
2) Where to put the code
- Theme: functions.php (for hooks) or in a template file for a specific loop.
- Plugin: create a tiny mu-plugin or functionality plugin if you need the behavior to survive theme changes.
3) Apply ordering only to selected queries (recommended)
If you want to apply the menu_order ordering only to a specific query (for example the main blog/home page), and not change all queries globally. Use pre_get_posts:
add_action( 'pre_get_posts', function( $query ) {
// safety checks: only modify frontend main query
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// apply only on the blog home page
if ( $query->is_home() ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' );
// optionally force posts_per_page
// $query->set( 'posts_per_page', 10 );
}
});
Common conditional uses: is_home(), is_post_type_archive(‘feature’), is_tax(), is_singular(), etc.
If you need only one custom WP_Query instance to respect the order, set orderby => ‘menu_order’ directly on that call instead of relying on global hooks.
4) Using Post Types Order with the REST API
Problem: WP REST API’s orderby parameter does not include menu_order by default for many post types. That means a request like GET /wp-json/wp/v2/posts?orderby=menu_order may be rejected or ignored.
Solution: add menu_order to the allowed orderby values for the REST collection. Hook into rest_{$post_type}_collection_params (or rest_post_collection_params for posts) and append menu_order.
/**
* Allow orderby=menu_order in the REST API for posts.
*/
function my_rest_add_menu_order( $params ) {
if ( isset( $params['orderby']['enum'] ) && is_array( $params['orderby']['enum'] ) ) {
$params['orderby']['enum'][] = 'menu_order';
}
return $params;
}
add_filter( 'rest_post_collection_params', 'my_rest_add_menu_order' );
// for custom post type 'feature' use rest_feature_collection_params
add_filter( 'rest_feature_collection_params', 'my_rest_add_menu_order' );
After adding this you can call:
GET /wp-json/wp/v2/feature?orderby=menu_order&order=asc
(If you use Advanced Post Types Order it already provide the same behavior automatically.)
5) Programmatically set or update menu order
If you need to set or change positions by code (bulk import, migration), update each post’s menu_order using wp_update_post():
// Re-order an array of post IDs to match a desired sequence
$post_ids = array( 12, 45, 23, 9 );
$position = 0;
foreach ( $post_ids as $post_id ) {
wp_update_post( array(
'ID' => $post_id,
'menu_order' => $position,
) );
$position++;
}
Important: run this in a safe context (CLI, admin request, or WP-CLI) for large lists and disable expensive actions or cache flushes while bulk updating.
6) Troubleshooting & caching
- No visible changes after reordering?
- Clear any page cache / object cache (Redis, Memcached). Plugins like WP Super Cache, W3 Total Cache, and host-level caches can hold old HTML.
- If you used query_posts() (discouraged) or get_posts() with suppress_filters => true, the plugin’s filters may be bypassed; use suppress_filters => false or a proper WP_Query.
- Check the plugin setting AutoSort / Admin Auto Apply Sort — enabling this applies the saved order automatically without code.
- Next / Previous links not matching the new order?
- Use previous_post_link() and next_post_link() (they will respect the plugin if the plugin option that applies next/previous is enabled). Otherwise use manual arrays of post IDs built from a menu_order query.
- Only some queries are sorted:
- If a theme/plugin creates a custom query and sets suppress_filters to true, the plugin won’t be able to modify the SQL. Modify that query to set suppress_filters => false or add orderby => ‘menu_order’ explicitly.
7) Advanced: ignore AutoSort for specific queries
If AutoSort is ON but you want to prevent the plugin from altering a specific query, use the filter the plugin exposes to ignore certain queries. Example filter (plugin provides pto/posts_orderby/ignore):
// ignore Post Types Order autosort for queries containing post_type 'reports'
add_filter( 'pto/posts_orderby/ignore', 'my_pto_ignore', 10, 3 );
function my_pto_ignore( $ignore, $orderBy, $query ) {
$pt = isset( $query->query_vars['post_type'] ) ? $query->query_vars['post_type'] : '';
if ( ( ! is_array( $pt ) && $pt === 'reports' ) || ( is_array( $pt ) && in_array( 'reports', $pt ) ) ) {
return true; // skip plugin ordering for this query
}
return $ignore;
}
If you use Advanced Post Types Order, you can use the visual conditionals.
8) Performance notes & best practices
- Prefer posts_per_page limited rather than -1 if the front-end does not need every post. Fetching thousands of posts at once can be slow and memory intensive.
- For very large lists, store the ordered list of IDs and page the front-end requests against that list (or a transient/cache) instead of running heavy SQL every request.
- Avoid modifying menu_order for frequently updated posts programmatically on every request; update only on reorder events.
9) Full example snippets
Example A — WP_Query within a template
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'orderby' => 'menu_order title', // secondary sort by title
'order' => 'ASC',
'posts_per_page' => 12,
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
wp_reset_postdata();
endif;
Example B — REST API orderby allow + default order for a CPT
// allow menu_order as orderby
add_filter( 'rest_feature_collection_params', function( $params ) {
$params['orderby']['enum'][] = 'menu_order';
return $params;
} );
// optional: force default order for REST collection (filter returns args before query)
add_filter( 'rest_feature_query', function( $args, $request ) {
if ( empty( $request['orderby'] ) ) {
$args['orderby'] = 'menu_order';
$args['order'] = 'ASC';
}
return $args;
}, 10, 2 );
FAQ
Q — I reordered posts but page still shows the old order.
A — Flush caches and make sure your template query is not overriding the order or suppressing filters.
Q — Does this work with pages?
A — Yes. WordPress pages use menu_order by default; Post Types Order extends this concept to posts and custom post types.
Q — Will this survive theme switches?
A — Yes, because the order is stored in the DB (menu_order). Reordering is independent of the theme. If you add code to functions.php, prefer placing it in a small functionality plugin if you will swap themes.
For advanced queries and sorts see the Advanced Post Types Order and it’s Documentation.
Hello, can you show exactly where should I put this code?
Hi,
Within your theme / custom code, you should first find the query you indent to apply the custom sorting capability. Then insert the ‘orderby’ => ‘menu_order’ .Or use the code above if you build a section from ground.
Thanks
Can you please answer the question? He asked you exactly where this code goes.
Hi,
There is no specific place where to place a code within your theme, it’s just depends on it’s structure and requirements. However the majority prefer the functions.php located within the theme directory root.
Thanks
How would you edit the code to target only the home page feed and not all the recent posts widgets around my site? Thank you!
i would check if the default_query is a feed e.g.
$wp_query->is_feed() see https://codex.wordpress.org/Conditional_Tags
Can you use this for the Wordpress REST API?
Perfectly done. Thankyou
Hi, The Post Re-order does not seem to be working on my blog. I’ve installed it. I have then reordered my posts through the drag and drop option, saved, and then refreshed my blog, but the posts have not changed positions? Please help. thanks http://www.bossladies.co.za
I had the same issue just now but clearing my cache helped
As Chin said, can you check if your blog use a cache plugin?
Hi,
the plugin is working excellent on my blog, but I’m experiencing a problem with the ‘next’ and ‘previous’ arrows below my posts.
I have this code to check if the post must hide or dispay the ‘next’ or the ‘previous’ arrow;
$cur_index = array_search( $post->ID, $portfolio_array );
if ( $cur_index > 0 ) {
$post_id = $portfolio_array[$cur_index-1];
…
echo ‘‘ . esc_html__( ‘Previous Project’, ‘alpha’ ) . ” . ” . get_the_title( $post_id ) . ‘‘;
}
if ( $cur_index + 1 < sizeof( $portfolio_array ) ) {
$post_id = $portfolio_array[$cur_index+1];
…
echo '‘ . esc_html__( ‘Next Project’, ‘alpha’ ) . ” . ” . get_the_title( $post_id ) . ‘‘;
}
How can I change the code so the arrows are displayed correctly? (and link to the correct post?
tia!
Presuming the $portfolio_array variable contain the objects archive, they are not retrieved in the order you set-up. I suggest so you check with this link on how to retrieve a list of objects in the defined order https://www.nsp-code.com/sample-code-on-how-to-apply-the-sort-for-post-types-order-plugin/
As alternative, you can try the following:
1) Make sure the plugin setting “Next / Previous Apply” is active.
2) Use the previous / next_post_link() to output the links, through which the order is applied automatically
https://codex.wordpress.org/Function_Reference/previous_post_link
https://codex.wordpress.org/Template_Tags/next_post_link
Thanks
Hi,
Thank you for your reply!
The solution was embarrassingly simple: I have fixed the issue by setting the ‘Blog pages show at most’ in the Wordpress Settings to more than 10… Duh…
cheers
Or you can use ‘posts_per_page’ => -1 within your query arguments.
Thanks
Could someone help me.. the plug in is working and allowing me to drag and drop the posts.. my code to display them is:
query_posts(‘cat=1’);
while (have_posts()) : the_post();
then I display parts in divs.. anyway the sort seems to have no effect.. I tride changing the above to:
query_posts(‘cat=1&orderby=menu_order’);
while (have_posts()) : the_post();
with no effect.. any advice is appreciated.
Steve
Try to include the ‘suppress_filters’ => FALSE within arguments, or better rely on full custom queries, see https://codex.wordpress.org/Class_Reference/WP_Query
Or make sure the plugin setting Auto Sort is turned on.
Hi,
Im using this plugin, I just wish this plug in should have that simple option to reverse the sort order based on a post_date. But i didnt find it.
i just want to change Sort order based on Post_date, ASC order.
I’m not sure in which file i need to change and code for it.
Could you please help on it.
Thank you.
Hi,
Sorry but the plugin apply the custom defined order. If you need to order by date, descending, you should just update your query arguments. Obliviously there is no specific file where to change, this is unique for each theme/plugin.
Thanks