admin管理员组文章数量:1134248
I have the following code, however, regardless of the numberpost
in $args
, I always get the default number of records set in my settings which is 6 and I have 200+ records. Is there any simple way to disable this or to override the numberposts in settings?
function fs_ski_resorts_data(){
$args = array(
'numberposts' => 999,
'post_type' => 'fs_ski_resorts',
);
$the_query = new WP_Query( $args );
$fs_ski_resorts = get_posts($args);
$fs_ski_resorts_data = [];
$i = 0;
while( $the_query->have_posts() ) : $the_query->the_post();
$fs_ski_resorts_data[$i]['fs_ski_resort_name'] = get_field('fs_ski_resort_data_fs_name') ?: get_field('fs_ski_resort_data_osm_name');
$i++;
endwhile;
return $fs_ski_resorts_data;
}
add_action('rest_api_init', function(){
register_rest_route('fs/v1', 'fs_ski_resorts', [
'methods' => 'GET',
'callback' => 'fs_ski_resorts_data',
]);
});
I have the following code, however, regardless of the numberpost
in $args
, I always get the default number of records set in my settings which is 6 and I have 200+ records. Is there any simple way to disable this or to override the numberposts in settings?
function fs_ski_resorts_data(){
$args = array(
'numberposts' => 999,
'post_type' => 'fs_ski_resorts',
);
$the_query = new WP_Query( $args );
$fs_ski_resorts = get_posts($args);
$fs_ski_resorts_data = [];
$i = 0;
while( $the_query->have_posts() ) : $the_query->the_post();
$fs_ski_resorts_data[$i]['fs_ski_resort_name'] = get_field('fs_ski_resort_data_fs_name') ?: get_field('fs_ski_resort_data_osm_name');
$i++;
endwhile;
return $fs_ski_resorts_data;
}
add_action('rest_api_init', function(){
register_rest_route('fs/v1', 'fs_ski_resorts', [
'methods' => 'GET',
'callback' => 'fs_ski_resorts_data',
]);
});
Share
Improve this question
asked Aug 11, 2023 at 3:31
spreadermanspreaderman
1138 bronze badges
1
- numberposts Is not a supported parameter of WP_Query. You need to use posts_per_page. but be warned that having an endpoint that queries 1000 posts is dangerous, and potentially makes it very easy to bring your site down. Your endpoint should implement pagination and your application should be built to work with that. – Jacob Peattie Commented Aug 11, 2023 at 4:27
1 Answer
Reset to default 2When calling WP_Query
directly, you should use posts_per_page
and not numberposts
— it is an alias for posts_per_page
, but only used with get_posts()
.
// These work, but I strongly advise against using 999:
$fs_ski_resorts = get_posts( array( 'numberposts' => 999 ) );
$fs_ski_resorts = get_posts( array( 'posts_per_page' => 999 ) );
$the_query = new WP_Query( array( 'posts_per_page' => 999 ) );
// This doesn't work:
$the_query = new WP_Query( array( 'numberposts' => 999 ) );
As for disabling the pagination, via WP_Query
, it can be done using the nopaging
parameter, but I suggest you to always enable pagination, which defaults to max 100 posts/items per page in the REST API. See https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/#pagination-parameters:
Large queries can hurt site performance, so
per_page
is capped at 100 records. If you wish to retrieve more than 100 records, for example to build a client-side list of all available categories, you may make multiple API requests and combine the results within your application.
本文标签: pluginsHow to disable pagination in Wordpress39 API results
版权声明:本文标题:plugins - How to disable pagination in Wordpress' API results? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736843928a1955219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论