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
Add a comment  | 

1 Answer 1

Reset to default 2

When 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