Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter gmolo

    (@gmolo)

    I never found anything that corrected the faulty get_posts(). So I had to create my own workaround:

    This code shows only posts and filters out the pages that incorrectly return from the get_posts() method by continuing to increment the offset until the full count is reached of just posts.

    // construct query
        $params = array(
        	'numberposts' => $numberposts,
        	'offset' => $offset,
        	'post_type' => 'post'    // doesn't work?
        	);
        $posts = get_posts($params);
    
        // filter out pages
        $pageCount = $pages = 0;
        $filteredPosts = array();
        do
        {
            $pageCount = 0;
    	foreach($posts as $p)
    	{
    	    if (strcmp($p->post_type, 'page') == 0)
    	    	++$pageCount;
    	    else
    	    	array_push($filteredPosts, $p);
    	}
    	if ($pageCount != 0)
    	{
    	    $offset += $numberposts;
    	    $numberposts = $pageCount;
    	    $params['numberposts'] = $numberposts;
    	    $params['offset'] = $offset;
    	    $posts = get_posts($params);
    	}
    	$pages += $pageCount;
        } while ($pageCount != 0);
    
        // Final array
        return $filteredPosts;
Viewing 1 replies (of 1 total)