Title: gmolo's Replies | WordPress.org

---

# gmolo

  [  ](https://wordpress.org/support/users/gmolo/)

 *   [Profile](https://wordpress.org/support/users/gmolo/)
 *   [Topics Started](https://wordpress.org/support/users/gmolo/topics/)
 *   [Replies Created](https://wordpress.org/support/users/gmolo/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/gmolo/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/gmolo/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/gmolo/engagements/)
 *   [Favorites](https://wordpress.org/support/users/gmolo/favorites/)

 Search replies:

## Forum Replies Created

Viewing 1 replies (of 1 total)

 *   Forum: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
   
   In reply to: [get_posts won't filter by post_type](https://wordpress.org/support/topic/get_posts-wont-filter-by-post_type/)
 *  Thread Starter [gmolo](https://wordpress.org/support/users/gmolo/)
 * (@gmolo)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/get_posts-wont-filter-by-post_type/#post-1902109)
 * 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)