WordPress.org

Ready to get started?Download WordPress

Forums

get_posts won't filter by post_type (5 posts)

  1. gmolo
    Member
    Posted 3 years ago #

    I've looked for bugs and support issues. The closest issue I found was this post here.

    I'm working with custom xml-rpc methods and using get_posts as follows:

    // construct query
        $params = array(
    	'post_type' => 'page',
         	'numberposts' => $numberposts,
        	'offset' => $offset
        	);
        $posts = get_posts($params);

    The returned value is an array of both posts and pages, though the other parameters work correctly. The resultant value of $posts is unchanged if I switch 'post_type' to => 'post' or omit it entirely (function reference claims to default to 'post').

    I've also tried a single string of args, but no difference:
    get_posts('post_type=post');

    If there's no solutions I'll

  2. status4
    Member
    Posted 3 years ago #

    Hey, did you find a solution?

  3. gmolo
    Member
    Posted 3 years ago #

    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;
  4. status4
    Member
    Posted 3 years ago #

    Hey, this is what I am doing at the moment too. But I thought there would be a "better" solution. Thanks anyway for your reply

  5. mighty_mt
    Member
    Posted 2 years ago #

    I was just having the same problem with the get_posts function. What I ended up doing is using WP_Query instead, which does exactly what get_posts should be doing as well.

    Here is a simplified version of my code:

    //Setting up the query arguments
    $myArgs = array( 'numberposts' => -1, 'post_type' => 'my_post_type', 'orderby' => 'title', 'order' => 'ASC' );
    //The following line doesen't work
    //$myPages = get_posts( $myArgs );
    //This does the job
    $myQuery = new WP_Query( $myArgs ); 
    
    //Looping through the posts
    while ( $myQuery->have_posts() ) : $myQuery->the_post();
    	echo '<a href="' .  get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();

Topic Closed

This topic has been closed to new replies.

About this Topic