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)

comment:1 @ryan7 years ago

Possibly due to #6772

comment:2 @ryan7 years ago

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

comment:3 @santosj7 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.

comment:4 @DD327 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 />';

comment:5 @slambert7 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.

comment:6 @slambert7 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.

comment:7 @ryan7 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.

comment:8 @rejon7 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...

comment:9 @rejon7 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?

comment:10 @rejon7 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...

comment:11 @rejon7 years ago

  • Cc jon@… added

comment:12 @rejon7 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...

comment:13 @rejon7 years ago

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

comment:14 @jacobsantos7 years ago

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

comment:15 @jacobsantos7 years ago

  • Keywords needs-testcases added

comment:16 @jacobsantos7 years ago

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

comment:17 @ryan7 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.

comment:18 @pyadav7 years ago

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

comment:19 @westi7 years ago

  • Milestone changed from 2.6.1 to 2.6.2

2.6.1 has been released, moving to 2.6.2 milestone

@ryan7 years ago

Dump get_posts() query object.

comment:20 @ryan7 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.

comment:21 @tandilboy7 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; ?>

@tandilboy7 years ago

query result

comment:22 @ryan7 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().

@tandilboy7 years ago

WP Query -Plugin Disabled-

comment:23 @tandilboy7 years ago

i disabled the plugin check the new file

comment:24 @ryan7 years ago

That looks better. Does it work?

comment:25 @tandilboy7 years ago

disabling the plugin isnt works....

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

comment:26 @ryan7 years ago

Are the posts in that category published?

comment:27 @tandilboy7 years ago


is a plugin error U_U


@ryan7 years ago

Suppress filters for get_posts()

comment:29 @ryan7 years ago

Try 7326.diff.

comment:30 @ryan7 years ago

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

comment:31 @DD327 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)

@DD327 years ago

comment:32 @DD327 years ago

attachment 7326.2.diff added.

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

comment:33 @ryan7 years ago

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

comment:34 @tandilboy7 years ago

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

comment:35 @ryan7 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

comment:36 @SergeyBiryukov4 years ago

  • Keywords needs-unit-tests removed

comment:37 @SergeyBiryukov4 years ago

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