Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You can use the "rest_{$this->post_type}_query" filter to alter the query args used. Setting ‘posts_per_page’ to -1 will get all pages. This filter is applied to all API requests of a particular post type, so your filter callback needs to differentiate the edit screen’s page parent request from all others. There’s likely something about either the passed args or the passed request object that distinguishes it from other requests.

    Thread Starter Dave McHale

    (@dmchale)

    Thanks a bunch @bcworkz that helped! Finding the syntax for the naming of the magic filters was tripping me up.

    Unfortunately using a value of -1 causes an error in the REST response
    {"code":"rest_post_invalid_page_number","message":"The page number requested is larger than the number of pages available.","data":{"status":400}}

    But knowing I have between 500 and 600 pages in the system, I set posts_per_page to 2000 and it works.

    I implemented a VERY rudimentary protection on the filter, to at least ensure it only adjusts the pagination if the request comes from a user who is logged in. Since we don’t support frontend users, this won’t cause a problem for us. Here’s what I ended up with, in case anyone can point out any errors or else finds this useful themselves. Hopefully someday this bug will be fixed and a hack like this becomes unnecessary.

    Final caveat: I haven’t finished halo testing this update, so I’m hoping it does not cause any unexpected issues elsewhere. But it did fix the issue I was trying to solve! I’ll update again if I find any issues.

    
    function my_custom_parent_dropdown_limit( $args, $request ) {
    
        // Only increase pagination if someone who is logged into the system is doing the REST request
        if ( is_user_logged_in() ) {
    
            // If you set this to -1, it throws a 500 error saying the requested page is higher than the number of pages.
            // But if we set it to a LOT of pages (eg: 2000 when we only have 500 pages), it works without errors.
            $args['posts_per_page'] = 2000;
    
        }
    
        return $args;
    }
    add_filter('rest_page_query', 'my_custom_parent_dropdown_limit', 20, 2);
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to filter Parent Page `posts_per_page` value in Gutenberg editor’ is closed to new replies.