• Skip to primary navigation
  • Skip to main content

Ishan Sharma

Full Stack Engineer, Blogger, Student

  • Blog
  • WordPress Experts
  • Contact
  • About

Making WP REST API Return More Than 100 Results

WordPress · June 20, 2017

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.

Share this:

  • Twitter
  • Facebook
  • Reddit
  • More
  • Email
  • Print
  • LinkedIn
  • Pocket
  • Pinterest
  • Tumblr

Filed Under: WordPress Tagged With: WordPress, WP REST API

Ishan

I am a Software Engineer and Writer. Interested in design, AI and drawing. When free, mostly found reading or gaming.

Reader Interactions

Comments

  1. Shawn Hooper says

    November 20, 2018 at 4:37 AM

    Thank you, this is exactly what I was looking for!

    Reply
    • Mohanavamsi says

      December 1, 2018 at 6:59 PM

      Guys i having same problem. can you show

      “In my case, here’s the code I ended up with” where i need to update this code ?

      Reply
      • Nate says

        December 9, 2018 at 8:31 PM

        I’d also like to know

        Reply
  2. Jachu says

    March 13, 2019 at 6:39 AM

    Hi,

    not working in wordpress 5.1… any solution?

    Reply
    • Jachu says

      March 13, 2019 at 6:54 AM

      Sorry 🙂 my mistake

      I needed rest_{$taxonomy}_collection_params hook 🙂 now work..

      Reply

Leave a Reply to Nate Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2010-2019 Ishan Sharma

Copyright © 2019 · Maker Pro on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.