Monday, December 5, 2022
HomeWordPress DevelopmentHow can I set the default 'orderby' and 'order' parameters for a...

How can I set the default ‘orderby’ and ‘order’ parameters for a REST API name?


I’ve added ‘menu_order’ to the accessible orderby strategies by utilizing the Easy Customized Put up Order plugin and by following the directions in this weblog put up, which gives the next code:

<?php
/**
 * The filter is called rest_{post_type}_collection_params. So it's good to hook a brand new filter for every 
 * of the customized put up varieties it's good to kind.
 * @hyperlink https://www.timrosswebdevelopment.com/wordpress-rest-api-post-order/
 */

// This allows the orderby=menu_order for Posts
add_filter( 'rest_post_collection_params', 'filter_add_rest_orderby_params', 10, 1 );
// And this for a customized put up kind known as 'portfolio'
add_filter( 'rest_portfolio_collection_params', 'filter_add_rest_orderby_params', 10, 1 );

/**
 * Add menu_order to the checklist of permitted orderby values
 */
operate filter_add_rest_orderby_params( $params ) {
    $params['orderby']['enum'][] = 'menu_order';
    return $params;
}

I’ve tacked that code onto a domestically developed site-specific plugin. This works wonderful and I can get the posts within the order I need utilizing:

.../wp-json/wp/v2/posts&order=asc&orderby=menu_order

How can I make that the default in order that

.../wp-json/wp/v2/posts

returns the posts in the identical order as above?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments