Created
March 29, 2019 12:14
-
-
Save felipeelia/db5ecc36885bb3cbde5334b06861ea78 to your computer and use it in GitHub Desktop.
Randomize the content list in REST API passing `orderby=rand` as parameter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment