0

I have a table named views.
this table has "post id" and "views"
I've added views for each post to my custom post type rest-API.
I want to order the rest-API posts by views.
I've found this topic that shows how to order the rest-API posts by meta value:
https://stackoverflow.com/questions/44783932/wp-rest-api-orderby-meta-value
but I want to order by a field that's not in the postmeta table.
Please tell me how can I do this.

0

This is the solution in the mentioned link if we want to use meta value to order the rest API content:

// Add meta your meta field to the allowed values of the REST API orderby parameter
add_filter(
    'rest_' . $post_type . '_collection_params',
    function( $params ) {
        $params['orderby']['enum'][] = 'YOUR_META_KEY';
        return $params;
    },
    10,
    1
);

// Manipulate query
add_filter(
    'rest_' . $post_type . '_query',
    function ( $args, $request ) {
        $order_by = $request->get_param( 'orderby' );
        if ( isset( $order_by ) && 'YOUR_META_KEY' === $order_by ) {
            $args['meta_key'] = $order_by;
            $args['orderby']  = 'meta_value'; // user 'meta_value_num' for numerical fields
        }
        return $args;
    },
    10,
    2
);

But I want to order the rest API contents by a field that's not in the meta table.

the table structure:

id views
-- -----
1   5
2   2
3   20
4   1

I want to sort the rest API posts descending by views in this table.
to do this I've edited Manipulate query section so I've created an array that has ids sorted by views. (to do the sort I've used the solution in this post)
then I've used post__in in args with the array that I've created before as its parameter and then I've used post__in as orderby parameter:

// Manipulate query

add_filter(
    'rest_' . $post_type . '_query',
    function ( $args, $request ) {
        //create an array that have ids that are sorted by views
        global $wpdb;   
        $post_views = $wpdb->get_results( $wpdb->prepare("SELECT id, views FROM " . $wpdb->prefix . "mytable") );
        $views = array_column($post_views, 'views');
        array_multisort($views, SORT_DESC, $post_views);
        $most_viewd = array_column($post_views, 'id');  
    
        $order_by = $request->get_param( 'orderby' );
        if ( isset( $order_by ) && 'views' === $order_by ) {
            $args['post__in'] = $most_viewd;
            $args['orderby']  = 'post__in';
        }
        return $args;
    },
    10,
    2
);

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.