Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active July 28, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/daac4798052afb0f13a6949ed0ed7506 to your computer and use it in GitHub Desktop.
Save Shelob9/daac4798052afb0f13a6949ed0ed7506 to your computer and use it in GitHub Desktop.
<?php
add_action( 'rest_endpoints', function( $endpoints ){
if( isset( $endpoints[ 'wp/v2/posts' ] ) ){
foreach( $endpoints[ 'wp/v2/posts' ] as &$post_endpoint ){
if( 'GET' == $post_endpoint[ 'METHOD' ] ){
$post_endpoint[ 'args' ][ 'type' ] = array(
'description' => 'Post types',
'type' => 'array',
'required' => false,
'default' => 'post'
);
}
}
}
return $endpoints;
}, 15 );
<?php
add_action( 'rest_post_query', function( $args, $request ){
$post_types = $request->get_param( 'type' );
if( ! empty( $post_types ) ){
if( is_string( $post_types ) ){
$post_types = array( $post_types );
foreach ( $post_types as $i => $post_type ){
$object= get_post_type_object( $post_type );
if( ! $object || ! $object->show_in_rest ){
unset( $post_types[ $i ] );
}
}
}
$args[ 'post_type' ] = $post_types;
}
return $args;
}, 10, 2 );
<?php
add_action( 'rest_post_query', function( $args, $request ){
$post_types = $request->get_param( 'type' );
if( ! empty( $post_types ) ){
if( is_string( $post_types ) ){
$post_types = array( $post_types );
foreach ( $post_types as $i => $post_type ){
$object= get_post_type_object( $post_type );
if( ! $object || ! $object->show_in_rest ){
unset( $post_types[ $i ] );
}
}
}
$post_types[] = $args[ 'post_type' ];
$args[ 'post_type' ] = $post_types;
}
return $args;
}, 10, 2 );
<?php
add_action( 'rest_endpoints', function( $endpoints) {
foreach ( $endpoints as $endpoint => $args ){
if( false !== strpos( '/wp/v2/users', $endpoint ) ){
unset( $endpoints[ $endpoint ] );
}
}
return $endpoints;
});
<?php
/** @var WP_REST_Request $request */
$type = $request->get_param( 'type' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment