0

Hello there for a custom post type "organizations" I have about 5000 posts which have to be shown in a list with link within one page.

However WP_Query just crashes (without PHP timeout just after 4 seconds on the server). Is this a WordPress problem? If so, why do they limit it? Or is it definitly a server problem?

$q = new WP_Query(array('post_type' => 'organization','posts_per_page' => 9999));
4
  • 1
    WordPress doesn't limit anything, but your server has memory limits.
    – Milo
    Commented Dec 21, 2015 at 20:14
  • 1
    Check your server logs. I'd bet that it isn't WP_Query crashing, but your MySQL server-- or the MySQL server or PHP is hitting a limit. Just because something happens when running WordPress doesn't mean that WordPress did it.
    – s_ha_dum
    Commented Dec 21, 2015 at 20:55
  • 1
    This sounds like a bad user experience to dump many thousands of records to the user at once - there are alternatives to this.
    – birgire
    Commented Dec 21, 2015 at 22:06
  • 1
    I totally agree with @birgire here. If I need to work through 5000 links, I would just close the page and never come back to your site. It is really really frustrating having to wait for a page to load just to find that it consist of 5000 links. This not only chase potential customers away, but slow page loads are really bad for SEO, if you are trying to build a site that should list in the top ten, you are definitely on the wrong road here, you might end up in the bottom 10 just on the extremely slow page load. You need to rethink your whole design. That is just my 2 cents Commented Dec 22, 2015 at 7:35

2 Answers 2

2

Querying this many posts is dangerous and you can ( and do ) take the site down. If you absolutely have to do this, you need to adjust your query to be more performant.

If you're just needing the link then add 'fields' => 'ids' to your params - this will only return the post ID and not the whole Post object. You can then use the ID to get the permalink, title etc.

If you're not going to paginate, then use 'no_found_rows' => true this will stop WordPress from running expensive SQL CALC queries to get the total number of posts that the query retrieves.

You should 100% cache the results of this query using the Transient API this will allow you to return cached HTML instead of rebuilding this query every time someone hits the page.

1
  • Good answer especially the Transient API was previously unknown to me. For now I solved the problem using $wpdb->get_results on IDs and title only which works just fine. However you are right that caching is highly recommendable in this case.
    – Blackbam
    Commented Dec 22, 2015 at 13:05
1

You're probably hitting your DB server's memory limits whenever you're running the query.

Why are you trying to show 5k results on one page? That seems like a really suboptimal idea from a UX standpoint to me.

1
  • The customers wish is not necessarily my wish ;-)
    – Blackbam
    Commented Dec 22, 2015 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.