WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 3 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:

Description

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) :
setup_postdata($post);
?>

Attachments (5)

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

Download all attachments as: .zip

Change History (42)

comment:1 ryan6 years ago

Possibly due to #6772

comment:2 ryan6 years ago

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

comment:3 santosj6 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 DD326 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 slambert6 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:

		<?php
		$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 slambert6 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 ryan6 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 rejon6 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:

http://svn.fabricatorz.com/fabricatorz_web/trunk/plugins/fabricatorz/fabricatorz.php

Then its called in template:

http://www.svn.overlap.org/overlap_web/trunk/themes/overlap/index.php

get_priority_box('CAT_NUMBERS_IN_COMMA_SEPARATED_LIST');

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

comment:9 rejon6 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 rejon6 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 rejon6 years ago

  • Cc jon@… added

comment:12 rejon6 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 rejon6 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 jacobsantos6 years ago

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

comment:15 jacobsantos6 years ago

  • Keywords needs-testcases added

comment:16 jacobsantos6 years ago

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

comment:17 ryan6 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 pyadav6 years ago

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

comment:19 westi6 years ago

  • Milestone changed from 2.6.1 to 2.6.2

2.6.1 has been released, moving to 2.6.2 milestone

ryan6 years ago

Dump get_posts() query object.

comment:20 ryan6 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 tandilboy6 years ago

the dump attached by me is for:

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

tandilboy6 years ago

query result

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

tandilboy6 years ago

WP Query -Plugin Disabled-

comment:23 tandilboy6 years ago

i disabled the plugin check the new file

comment:24 ryan6 years ago

That looks better. Does it work?

comment:25 tandilboy6 years ago

disabling the plugin isnt works....

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

comment:26 ryan6 years ago

Are the posts in that category published?

comment:27 tandilboy6 years ago

I CHECK THE ERROR....

is a plugin error U_U

sorry

ryan6 years ago

Suppress filters for get_posts()

comment:29 ryan6 years ago

Try 7326.diff.

comment:30 ryan6 years ago

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

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

DD326 years ago

comment:32 DD326 years ago

attachment 7326.2.diff added.

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

comment:33 ryan6 years ago

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

comment:34 tandilboy6 years ago

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

comment:35 ryan6 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 SergeyBiryukov3 years ago

  • Keywords needs-unit-tests removed

comment:37 SergeyBiryukov3 years ago

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