Created
March 21, 2016 20:38
-
-
Save abrudtkuhl/df73990cb37b426037c5 to your computer and use it in GitHub Desktop.
WordPress REST API - Get Random Post endpoint
This file contains hidden or 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: WP Slack REST API Backend | |
* Description: An example of using the WordPress REST API as a backend for a Slack Bot | |
* Author: Andy Brudtkuhl | |
* Author URI: http://youmetandy.com | |
* Version: 0.1 | |
* Plugin URI: https://github.com/abrudtkuhl/heykramer | |
* License: GPL2+ | |
*/ | |
add_action( 'rest_api_init', function () { | |
register_rest_route( 'api', '/any', array( | |
'methods' => 'GET', | |
'callback' => 'get_random', | |
) ); | |
} | |
function get_random() { | |
return get_posts( array( 'orderby' => 'rand', 'posts_per_page' => 1) ); | |
} |
rest_{post_type}_collection_params
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add
orderby => rand
to existing posts api endpoint:Now this works:
/wp-json/wp/v2/posts?orderby=rand