Forked from felipeelia/add_rand_orderby_restapi_post.php
Created
November 10, 2020 20:26
-
-
Save TardencillaRuiz/f049969fab56c0c40f48a9aca33db8a3 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