$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Using The REST API How To Pull All Custom Posts?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Using The REST API How To Pull All Custom Posts?

matteradmin8PV0评论

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.

Share Improve this question asked Jan 18, 2019 at 16:59 user9664977user9664977 3875 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far