Search code examples
wordpresssortingwordpress-rest-api

Ordering By Menu Order in the Wordpress REST API?


I am trying to display posts using the REST API and I want to display them in menu order based on how they are positioned in the admin menu.

I installed a plugin that allowed me to move posts to any position in the admin. I know querying the posts the normal way with wordpress there is a orderby: menu_order option, which would do what I am looking for, but I can't figure it out with the REST API.

My REST API looks like this:

https://example.com/wp-json/wp/v2/qd_leadership?_embed&per_page=100&orderby=menu_order

So I've tried that and that does not work. It says menu_order isn't an option. I also saw a post here:

Query WordPress (REST) posts by the order they appear on the admin

That had a similar question. The only answer on that post is to not have any orderby parameter and that should display them in the menu order, but that did not work for me. So I am stumped on how to order the posts from the REST API in menu order?


Solution

  • Its bug of wp core in rest api so you could use below hack for the solution.Please add below code in your active theme's function.php

    add_filter( 'rest_post_collection_params', 'my_prefix_add_rest_orderby_params', 10, 1 );
    
    function my_prefix_add_rest_orderby_params( $params ) {
        $params['orderby']['enum'][] = 'menu_order';
    
        return $params;
    }
    

    Tested and works.