1

I generally use the Intuitive Custom Post Order plugin for any WordPress sites I work with so clients can order their posts visually.

For this project, I'm using WordPress as an admin panel and consuming it's data with an Ember.js front-end.

(using wp-api V.2) The re-ordered order doesn't show in the JSON and I'm guessing the plugin stores the new order in an in-between piece of PHP.

Besides manually changing the date of each post, what are my options to allow my client an intuitive way to re-order posts in a way that persists?

  • what version of wp-api you use ?? – Ahed Eid May 4 '16 at 10:44
  • @Ahed Eid - Version 2 – sheriffderek May 4 '16 at 22:29
  • If it stores the new order into menu_order then one can order hierarchical post types with e.g. ?orderby=menu_order&order=asc – birgire May 4 '16 at 22:49
  • Your option is to write code that does it, or modify the plugin – Mark Kaplun May 5 '16 at 7:14
2

Let's assume it stores the custom order into the menu_order column in the wp_posts table.

If you mean the hierarchical page post type (supports page-attributes) then one can order with the query variables:

/wp-json/wp/v2/pages/?orderby=menu_order&order=asc

If you mean the post post type with:

/wp-json/wp/v2/posts/

there's a way using the rest_{post_type}_query filter:

/**
 * Set orderby to 'menu_order' for the 'post' post type
 */
add_filter( "rest_post_query", function( $args, $request )
{
    $args['orderby'] = 'menu_order';
    return $args;
}, 10, 2 );

We might want to restrict this further. Skimming through the WP_REST_Request class we can see there's a handy get_param() public method that we could make use of:

/**
 * Support for 'wpse_custom_order=menu_order' for the 'post' post type
 */
add_filter( "rest_post_query", function( $args, $request )
{
    if( 'menu_order' === $request->get_param( 'wpse_custom_order' ) )
        $args['orderby'] = 'menu_order';

    return $args;
}, 10, 2 );

where we activate it through a custom wpse_custom_order parameter:

/wp-json/wp/v2/posts/?wpse_custom_order=menu_order&order=asc

There's also the dynamically generated rest_query_var-orderby filter.

  • Thanks! That worked! Although do note that /wp-json/wp/v2/pages/?orderby=menu_order&order=asc won't work by default. I'm adding a filter using rest_<custom_post_type>_query – Jason Axelson Mar 26 '17 at 3:28
0

Ordering posts with menu_order as orderby parameter will give invalid parameter error e.g.

http://example.com/wp-json/wp/v2/posts?orderby=menu_order&order=asc

Invalid parameter(s): orderby (orderby is not one of date, id, include, title, slug)

At this time there's no filter to add another orderby option https://github.com/WP-API/WP-API/blob/2.0-beta13/lib/endpoints/class-wp-rest-posts-controller.php#L1666

you should hook at posts_clauses for example

/**
 * Class Custom_Rest
 * @author 
 */
class Custom_Rest
{
    /**
     * init 
     * Add Rest Hooks
     *
     * @return void
     * @author 
     */
    public static function init()
    {
        add_filter( 'posts_clauses' , 'Custom_Rest::orderby' );
    }


    /**
     * orderby
     * Order posts by menu_order
     *
     * @return void
     * @author 
     */
    public static function orderby($v) {

        if ( $GLOBALS['wp']->query_vars['rest_route'] == '/wp/v2/posts' ) {
            // old value is  "wp_posts.post_date DESC"
            $v['orderby'] = str_replace('post_date', 'menu_order', $v['orderby']);
            return $v;
        }

    }

}


add_action( 'rest_api_init', 'Custom_Rest::init' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.