Ordering By Menu Order in the Wordpress REST API? Ordering By Menu Order in the Wordpress REST API? wordpress wordpress

Ordering By Menu Order in the Wordpress REST API?


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.


Thanks to raju_eww for the hint in the right direction. But in case of a custom post type collection the filter hook name has to be like this:

add_filter( 'rest_custom-post-type_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;}

found here:https://www.timrosswebdevelopment.com/wordpress-rest-api-post-order/


Following code do sort by menu_order when orderby is not in query string (for woocommerce):

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');function am_woocommerce_catalog_orderby( $args ) {    if(!$_GET['orderby']) {    $args['orderby'] = 'menu_order';    $args['order'] = 'asc';    }    return $args;    }

source