With WordPress API, it is very convenient and easy to build web and mobile apps powered by WordPress. One limitation that you may run into is that the post type endpoints have a limit of 100 results. If you ask for more than 100 results, will return following error:
In some cases, you may want more than 100 results. In my case, I was building a web app using Angular and it needed to load all the customers on the page. Since there were only 150 or so customers and I knew that they would never cross 500 , I wanted to load all of them in one request and display later using data tables.
I could not find any resources that allowed me to override number of results. A bit of research lead me to rest_{$this->post_type}_query
hook. It executes only for the post type we mention, so that’s perfect for our use case.
In my case, here’s the code I ended up with:
The post type in my case is customer. So the hook becomes rest_customer_query
. $params
is just an array passed to our function that’s used for WP_Query
. You can modify it to alter results. I am setting $params['posts_per_page']
to PHP constant PHP_INT_MAX
to get all the results.
Have any better approach in mind? Do let me know via comments.