Custom Post Type returning only posts belonging to the Author in WordPress REST API

Add the following to your plugin code.
Replace ‘goals’ with your content type name.

function only_users_goals( $query ) 
{
	if ( $query->get( 'post_type' ) === 'goals' ) {
		$query->set( 'author', get_current_user_id() );
	}
}
add_action( 'pre_get_posts', 'only_users_goals' );

About this solution.

  • The great part about this is that you don’t need to create any custom endpoints to show only the related posts to the author.
  • Can apply very easily for any CUSTOM Posts.

Possible Scenarios where can you use it.

Suppose you are providing a solution where user wants the user to post and some entries into WordPress custom post type(CPT). there you can apply this solution.