• 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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter SpankMarvin

    (@spankmarvin)

    ok, so I’ve isolated the issue, but still have no idea how to resolve it. The client is using the WP Events Calendar plugin. Disabling this fixes the issue completely. Nothing wrong at all. The client is using only the shortcode aspect of the calendar plugin, I believe, but it’s still conflicting, even though I’m not using the plugin on that page.

    Not sure there’s going to be a simple solution to this. Since it’s not an error with the actual loop (to my knowledge) it’s a little trickier to get to the actual offending code in the plugin.

    Don’t suppose anyone has had a similar issue? Long shot…

    Sounds to me like that other plugin is manipulating the post count. The WP API and architecture surrounding the db is so janky that it’s no surprise that this kind of weird issue has come up. Again, it’s probably not a problem with the loop, but rather the other plugin.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_posts and numberposts=-1 breaking loops’ is closed to new replies.