Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The following code:

$params = array(
    'post_type' => 'game',
    'post_status' => 'publish',
    'posts_per_page' => -1,
);

$posts = get_posts($params);

works fine on my dev environment (MAMP), but when I try to run it on our staging environment (Apache on Linux) I get a 500 error (absolutely nothing in the source) with no error's in the error log.

When I narrow it down, I notice that if I set the posts per page to greater than 37 (we have approximately 60 posts on staging, from which I have recently updated my dev database from (meaning post 38 is in my dev as well)), all is well. All is also well when I set posts_per_page to 30, and add the parameter 'paged' => 2 (so post 38 isn't causing a problem). I have also tried setting posts_per_page to 40 and paged to 2 which currently works fine, but I suspect that is because we don't have over 80 posts.

Any idea's as to what else I could try, or what is likely to be the problem?

share|improve this question
Server is most likely running short of RAM. – Ijaas Nov 17 '11 at 10:42
Highly unlikely. when running the top command, the free memory is hovering around 250Mb. It's also not hitting the PHP memory limit as I have seen the server spit out the PHP out of memory limit (when I have activated another plugin on the site). – Brian Barnes Nov 17 '11 at 20:23
check your error logs. – Ijaas Nov 18 '11 at 23:49
Yep, done that - I made use of error_log to track down exactly where this fault is occurring. I'd use it to go deeper, but I don't understand the flow after that. – Brian Barnes Nov 20 '11 at 20:01
what did the error_log say exactly? – Ijaas Nov 21 '11 at 1:47
show 1 more comment

closed as too localized by toscho Jul 22 '12 at 23:12

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

im stumped.

try split it into multiple queries.

$count_posts = wp_count_posts('game');
$pages = $count_posts->publish/20;
$page = 0;

while($pages > 0){
    $pages--;
    $page ++;   

    $params = array(
        'post_type' => 'game',
        'paged' => $page,
        'posts_per_page' => 20
    );

    $posts = get_posts($params);
    //Loop here    
}
share|improve this answer
this isn't the most effective fix so be sure to use a cache plugin to reduce the number db queries. – Ijaas Nov 22 '11 at 7:42
That bumped up the number that I could do but unfortunately hasn't solved it. It does appear to be a memory issue (ini_set('MEMORY_LIMIT', '32M') fixes it) but even when I add define('WP_DEBUG', TRUE); (and get spammed by buddypress messages) I'm still not getting the standard PHP out of memory error (even in the logs). – Brian Barnes Nov 22 '11 at 23:57
Correction it hasn't changed anything (grr...). I've made it so that it can successfully return 2 pages (at 15 posts per page), but it still can't handle anything more than 38 posts. It looks like I'm going to have to up my memory limit (as much as I don't like it). – Brian Barnes Nov 23 '11 at 0:53
php error reporting must be turned off, see php.net/manual/en/function.error-reporting.php. If you on a shared server check with your host for the location of that error file. – Ijaas Nov 23 '11 at 1:51

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