Last active
March 1, 2022 13:29
-
-
Save MaximeCulea/6d7e5951fac72d707f647f4376d601b0 to your computer and use it in GitHub Desktop.
Add WordPress REST API tweeks
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 | |
/** | |
* Add other post types into the post rest api route to gather different contents type | |
* | |
* @link /wp-json/wp/v2/posts/ | |
* | |
* @param $args | |
* @param $request | |
* | |
* @return mixed | |
* @author Maxime CULEA | |
*/ | |
function mc_multiple_post_types( $args, $request ) { | |
$args['post_type'] = apply_filters( 'mc_multiple_add_post_types', [ $args['post_type'] ] ); | |
return $args; | |
} | |
add_filter( 'rest_post_query', 'mc_multiple_post_types', 20, 2 ); | |
/** | |
* Add event post types for the post route | |
* | |
* @param $post_types | |
* | |
* @return mixed | |
* @author Maxime CULEA | |
*/ | |
function mc_multiple_add_post_types( $post_types ) { | |
$post_types[] = 'event'; | |
return $post_types; | |
} | |
add_filter( 'mc_multiple_add_post_types', 'mc_multiple_add_post_types' ); | |
/** | |
* Add the future post status to the requested post type | |
* | |
* @param $args | |
* @param $request | |
* | |
* @return mixed | |
* @author Maxime CULEA | |
*/ | |
function mc_add_future_post_status( $args, $request ) { | |
$args['post_status'] = array_merge( $args['post_status'], [ 'future' ] ); | |
return $args; | |
} | |
add_filter( 'rest_post_query', 'mc_add_future_post_status', 10, 2 ); | |
add_filter( 'rest_event_query', 'mc_add_future_post_status', 10, 2 ); | |
/** | |
* Set Future status available for contents in the REST API | |
* Check if REST REQUEST is defined, works only for rest_api_init hook (instead of init) | |
* | |
* @author Maxime CULEA | |
*/ | |
function mc_future_post_status_for_rest_api() { | |
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) { | |
return; | |
} | |
global $wp_post_statuses; | |
$wp_post_statuses['future']->public = true; | |
$wp_post_statuses['future']->queryable = true; | |
} | |
add_action( 'rest_api_init', 'mc_future_post_status_for_rest_api', PHP_INT_MAX ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment