Search code examples
phpmysqlwordpresswordpress-rest-api

Get random posts through WordPress API


I am building a self-test project which can give 10 questions at one time from a question list. I want the 10 questions should be different every time I start the test. The front-end is React and the back-end is WordPress by using WordPress API.

Previously I used orderby=rand in the query by implementing a plug-in

<?php

/**

 * Plugin Name: REST API - Post list randomize

 * Description: Randomize the content list in REST API passing `orderby=rand` as parameter.

 * Version:     1.0.0

 * Author:      Felipe Elia | Codeable

 * Author URI:  https://codeable.io/developers/felipe-elia?ref=qGTOJ

 */



/**

 * Add `rand` as an option for orderby param in REST API.

 * Hook to `rest_{$this->post_type}_collection_params` filter.

 *

 * @param array $query_params Accepted parameters.

 * @return array

 */

function add_rand_orderby_rest_post_collection_params( $query_params ) {

    $query_params['orderby']['enum'][] = 'rand';

    return $query_params;

}

add_filter( 'rest_post_collection_params', 'add_rand_orderby_rest_post_collection_params' );

It worked perfectly until 2 weeks ago. Without modifying any code, it was just broken. I used Postman to test, such as http://localhost/wp/wp-json/wp/v2/questions?per_page=10&orderby=rand. The response is

    "code": "rest_invalid_param",
        "message": "Invalid parameter(s): orderby",
        "data": {
            "status": 400,
            "params": {
                "orderby": "orderby is not one of author, date, id, include, modified, parent, relevance, slug, include_slugs, title."
            }
        }

Two weeks ago if I used the same query, it could give me 10 random questions. It looks like the plug-in cannot add rand successfully as a parameter for orderby in WordPress like before.

BTW, the functionality of orderby=rand in WP isn't broken because if I manually add rand as a parameter in WP core code, the above query can work again.

Does anybody know what's wrong with the plug-in or some latest updates in WP causing the problem?

Another thing is I saw some articles mentioning ORDERBY = RAND() in MySQL will affect the performance severely when the database is large. So I wonder whether I should use orderby=rand in the query to get random questions or think about other ways to do the job. Does anybody have any suggestions for this performance issue? Thanks!


Solution

  • Found the answer. I need to make the first parameter rest_post_collection_params of add_filter function to the correspondent post type.

    The original rest_post_collection_params is for the WP default posts type. Because I use ACF(Advanced Custom Fields, another plug-in to create customized post types) to create my own post types, such as books, I need to change the first parameter to rest_books_collection_params. If you have more customized post types, just create as many as add_filter functions for each of them.

    Just have no ideas why I could use rest_post_collection_params for all my customized post types 3 weeks ago but not now. Anyway, since it is solved, don't bother. My project is more front-end oriented. WP is just storage.

    BTW, probably someone has noticed that rest_post_collection_params is for the WP default post type posts. In the parameter it uses single form post for the plural form posts. This only works for the WP default post type. For customized types, if it is books, the parameter should be rest_books_collection_param; if questions, then rest_questions_collection_param. Keep the parameter exactly the same as the post type.