get_posts and numberposts=-1 breaking loops
-
Hi there
I’ve been staring at this issue for ages now and cannot work out what is going wrong.
I have a custom post type, called factoids. Very simple. Inside my main loop in a page template, I am calling get_posts to access these factoids in random order. I want to have ALL factoids show on one page.
With 5 factoids in place, all worked absolutely fine. However, now that I have 29, it is completely breaking not only itself but the other loop too. Something appears to seize it up entirely. My code is as follows, and I have tried all manner of reset attempts (storing $post in a temp variable before reinstating after the foreach loop; calling wp_reset_query) to no avail:
$args = 'post_type=factoids&orderby=rand&numberposts=-1'; $facts = get_posts($args); echo(count($facts)); if($facts != ''){ $content = '<div class="fact-ov-cont"><h3 class="factoid-header">Did You Know?</h3> <div class="factoidss-container">'; foreach($facts as $fact){ $content .= '<div class="item"> <p class="factoid">'.$fact->post_title.'</p> </div>'; } $content .= '</div></div>'; } echo($content);
The count() function gives me a correct count when I choose a low number, and the factoids show then too. E.g. at the moment, if I give numberposts a value of 12, it works just fine.
I feel like I must be looking over something obvious. Is it somehow interfering with a global post count, or somehow interfering with the main loop? I had thought I’d tried everything to reinstate the loop as per the codex.
Any advice gratefully received.
J
- The topic ‘get_posts and numberposts=-1 breaking loops’ is closed to new replies.