What I am trying to do is pull back all posts of a custom post type. I know with the WordPress REST API you can add per_page=100 to the end of the REST API to pull back 100 posts, but I need a way to pull back ALL of the posts from a custom post type.
After doing some research I found this function:
function rest_posts_per_page( $args, $request ) {
$max = max( (int)$request->get_param( 'per_page' ), 1000 );
$args['posts_per_page'] = $max;
return $args;
}
add_filter( 'rest_post_query', 'rest_posts_per_page', 10, 2 );
Which switches the amount of posts pulled back from the REST API to 1000 by default.
This works great for the default post, but I need this to work with custom post types as well, since the above function only appears to work with the default post. So what I am trying to figure out is how what is needed for the above function to work with a custom post type instead of the default post.
What I am trying to do is pull back all posts of a custom post type. I know with the WordPress REST API you can add per_page=100 to the end of the REST API to pull back 100 posts, but I need a way to pull back ALL of the posts from a custom post type.
After doing some research I found this function:
function rest_posts_per_page( $args, $request ) {
$max = max( (int)$request->get_param( 'per_page' ), 1000 );
$args['posts_per_page'] = $max;
return $args;
}
add_filter( 'rest_post_query', 'rest_posts_per_page', 10, 2 );
Which switches the amount of posts pulled back from the REST API to 1000 by default.
This works great for the default post, but I need this to work with custom post types as well, since the above function only appears to work with the default post. So what I am trying to figure out is how what is needed for the above function to work with a custom post type instead of the default post.
The filter is dynamic
, i.e. generated from rest_{$post_type}_query
.
So for the post
post type it's rest_post_query
and for the cpt
post type it's rest_cpt_query
Note that REST pagination is available:
https://example/wp-json/wp/v2/posts?per_page=100&page=1
https://example/wp-json/wp/v2/posts?per_page=100&page=2
...
The maximum of per_page
is to reduce the risk of fatal errors if available resources are exceeded when too many items are fetched.