Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#7326 closed defect (bug) (fixed)

get_posts not working properly

Reported by: wpcandy Owned by: pyadav
Milestone: 2.6.2 Priority: high
Severity: blocker Version: 2.6
Component: General Keywords: needs-unit-tests
Focuses: Cc:


Here's an example of a get_posts code block that doesn't work. I don't think any get_posts queries are working.

<?php $lastposts = get_posts('numberposts=1&category=7&orderby=post_date&order=DESC');
foreach($lastposts as $post) :

Attachments (5)

get_posts_debug.diff (380 bytes) - added by ryan 7 years ago.
Dump get_posts() query object.
Query_result.txt (2.3 KB) - added by tandilboy 7 years ago.
query result
WP_Query_PluginDisabled.txt (2.3 KB) - added by tandilboy 7 years ago.
WP Query -Plugin Disabled-
7326.diff (5.0 KB) - added by ryan 7 years ago.
Suppress filters for get_posts()
7326.2.diff (2.2 KB) - added by DD32 7 years ago.

Download all attachments as: .zip

Change History (42)

#1 @ryan
7 years ago

Possibly due to #6772

#2 @ryan
7 years ago

I tried the same code and it seems to be working for me.

#3 @santosj
7 years ago

It is possible there are no posts in category 7 and there should only be one post, so the example code is unoptimized.

#4 @DD32
7 years ago

While i'm not using

foreach($lastposts as $post) : setup_postdata($post);

I've found get_posts() to be working ok:

	$posts = get_posts(array('tag__in' => array($tag->term_id), 'number' => 5 ));
	$html .= '<h4>Recent Posts</h4>';
	foreach ( (array)$posts as $post )
		$html .= '<a href="' . get_permalink($post) . '">' . get_the_title($post) . '</a><br />';

#5 @slambert
7 years ago

I also had a problem and might be able to provide more details.

I had two instances of

<?php $posts = get_posts('numberposts=#&category=#'); ?>

running in a home.php template. The first instance worked, the second did not. I tested, using:

<?php print "<!-- There are ".count($posts)." posts -->"; ?>

and 0 posts were found.

I tried using a different format:

		$args = array(
			'numberposts' => 6,
			'category' => 9,
		$posts = get_posts('');

And no posts were found.

I replaced the code with query_posts:

<?php $posts = query_posts( "cat=9&posts_per_page=6" ); ?>

And it worked as expected returning the posts.

This changed for me upon update from 2.5 to 2.6.

I hope this information helps.

#6 @slambert
7 years ago

also, forgot to mention - the first instance had text excerpts, the second had excerpts with image tags. Not sure if it's relevant, but thought I should include the info.

#7 @ryan
7 years ago

I put two get_posts() instances in a template and both return the same correct results. I think we'll need to see the actual templates to track this down.

#8 @rejon
7 years ago

  • Severity changed from normal to blocker

I'm having the same problem. The get_posts works fine without the category option in query string. I made a function to show a box at the top of the page highlighting a post. You can check it out from here to test out and call then in template at top like on an index.php template:


Then its called in template:



Let me know...investigating the query_posts approach...

#9 @rejon
7 years ago

I can confirm that replacing the get_posts function with query_posts above works as a drop-in replacement...is this get_posts function now deprecated or something?

#10 @rejon
7 years ago

Actually, this is not a fix since query_posts only allows one call per page and I need another call outside the loop which get_posts allows...this is big problem...need fix...

#11 @rejon
7 years ago

  • Cc jon@… added

#12 @rejon
7 years ago

Ok, tried alt. way to have arguments:

$myposts = get_posts(array('category' => '324',

'orderby' => 'post_modified',
'numberposts'=> 1 ) );

IMO, the problem is something with category above since all works fine if I remove the category arg. Ok, looking deeper...

#13 @rejon
7 years ago

Its most definitely in wp-includes/query.php in the class WP_query ... arg arg arg...anyone want to debug this bastard?

#14 @jacobsantos
7 years ago

Write test cases for how you think the get_posts() function should work like.

#15 @jacobsantos
7 years ago

  • Keywords needs-testcases added

#16 @jacobsantos
7 years ago

  • Keywords needs-unit-tests added; needs-testcases removed

#17 @ryan
7 years ago

rejon, I don't think get_posts() has ever accepted a comma separated list of categories. It expects a single category ID. The fabricatorz example is passing '324,7'.

You other example works fine for me. Sorry, but I can't reproduce any of these problems.

#18 @pyadav
7 years ago

  • Owner changed from anonymous to pyadav
  • Status changed from new to assigned

#19 @westi
7 years ago

  • Milestone changed from 2.6.1 to 2.6.2

2.6.1 has been released, moving to 2.6.2 milestone

7 years ago

Dump get_posts() query object.

#20 @ryan
7 years ago

If you apply that patch, get_posts() will dump its query object to the page. View source and cut-and-paste the "WP_Query Object" dump to a file and attach to this ticket.

#21 @tandilboy
7 years ago

the dump attached by me is for:

 global $post;
 $myposts = get_posts('numberposts=1&offset=0&category=24');
 foreach($myposts as $post) :
    <img src="<?php bloginfo('stylesheet_directory'); ?>/images/alerta.gif" alt="Destacada" /><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
 <?php endforeach; ?>

7 years ago

query result

#22 @ryan
7 years ago

They query looks fine until this "NOT IN (19, 20, 21, 22, 24)". I think you have a plugin that manipulates the query. Now that get_posts() uses WP_Query, it is subject to plugins that modify the query. We'll probably have to disable query filters when calling WP_Query from get_posts().

7 years ago

WP Query -Plugin Disabled-

#23 @tandilboy
7 years ago

i disabled the plugin check the new file

#24 @ryan
7 years ago

That looks better. Does it work?

#25 @tandilboy
7 years ago

disabling the plugin isnt works....

check the second WP-Query (the plugin disabled query) and please try to fix :(

#26 @ryan
7 years ago

Are the posts in that category published?

#27 @tandilboy
7 years ago


is a plugin error U_U


7 years ago

Suppress filters for get_posts()

#29 @ryan
7 years ago

Try 7326.diff.

#30 @ryan
7 years ago

(In [8766]) Suppress query filters when called from get_posts(). see #7326 #7547

#31 @DD32
7 years ago

(In [8766]) Suppress query filters when called from get_posts(). see #7326 #7547

After this get_posts() is returning null for me in a few instances, Also mentioned on wp-testers an issue similar.

It appears that when filters are supressed, certain fields are not being set. (See Patch)

7 years ago

#32 @DD32
7 years ago

attachment 7326.2.diff added.

Lines #781 & #837 are unrelated to the issue and fix undefined variable notices.

#33 @ryan
7 years ago

(In [8773]) Mike filter suppression. Props DD32. see #7326

#34 @tandilboy
7 years ago

i must use 7326.diff AND 7326.2.diff patches to fix the problem

#35 @ryan
7 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [8795]) Suppress query filters when called from get_posts(). fixes #7326 #7457 for 2.6

#36 @SergeyBiryukov
4 years ago

  • Keywords needs-unit-tests removed

#37 @SergeyBiryukov
4 years ago

  • Keywords needs-unit-tests added
Note: See TracTickets for help on using tickets.