How to increase the WPGraphQL query limit

 https://www.devtwins.com/blog/wpgraphql-increase-post-limit

WPGraphQL is a wonderful plugin that enables GraphQL querying of WordPress content, but unfortunately it limits the amount of posts you can query at a time to 100 by default.

In this post, we are going to show you the best way to increase this limit.

The main solution originally presented by Jason Bahl (the creator of WPGraphQL) is to paste this function into the bottom of your WordPress theme's functions.php file:

add_filter( 'graphql_connection_max_query_amount', function( $amount, $source, $args, $context, $info ) {
if(current_user_can( 'manage_options')) {
$amount = 1000; // increase post limit to 1000
}
return $amount;
}, 10, 5 );
// not the ideal solution. For best solution, see below.

However, an issue that users have noticed with this solution is that although it works inside of the GraphiQL plugin (inside of the WordPress instance), the 100 post limit often remains for outside services querying for your WordPress content (such as Vercel, Netlify, or Heroku).

The problem is with the statement: if(current_user_can( 'manage_options'). Outside queries will not satisfy this condition, so the new $amount will not be set.

So the best solution is to remove the if statement and just set the $amount directly no matter what:

// best solution
add_filter( 'graphql_connection_max_query_amount', function( $amount, $source, $args, $context, $info ) {
$amount = 1000; // increase post limit to 1000
return $amount;
}, 10, 5 );

Disclaimer: fetching too many things in one request can cause slowdowns for the client and possibly server performance issues as well. There's no magic number to what the right amount of posts to return for optimal performance, but the more data the server is processing and the more the client is downloading, the worse the performance tends to be. Sometimes you can get better performance from more, but smaller, requests.

Additional Notes

Make sure you paste the function above into the bottom of your wp-content/themes/[theme_name]/functions.php file.

If you have multiple themes and aren't sure which theme you're currently using, you can go to Appearance --> Themes and it will show you the currently active theme.

Không có nhận xét nào:

Cold Turkey Blocker

 https://superuser.com/questions/1366153/how-to-get-rid-of-cold-turkey-website-blocker-get-around-the-block Very old question, but still wan...