Ticket #7326 (closed defect: fixed)

Opened 5 months ago

Last modified 3 months ago

get_posts not working properly

Reported by: wpcandy Assigned to: pyadav
Priority: high Milestone: 2.6.2
Component: General Version: 2.6
Severity: blocker Keywords: needs-unit-tests
Cc: jon@rejon.org

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

get_posts_debug.diff (380 bytes) - added by ryan on 08/19/08 21:27:58.
Dump get_posts() query object.
Query_result.txt (2.3 kB) - added by tandilboy on 08/21/08 14:22:54.
query result
WP_Query_PluginDisabled.txt (2.3 kB) - added by tandilboy on 08/22/08 12:36:08.
WP Query -Plugin Disabled-
7326.diff (5.0 kB) - added by ryan on 08/22/08 21:21:48.
Suppress filters for get_posts()
7326.2.diff (2.2 kB) - added by DD32 on 08/29/08 10:48:35.

Change History

07/16/08 21:53:37 changed by ryan

Possibly due to #6772

07/16/08 21:58:58 changed by ryan

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

07/16/08 22:33:53 changed by santosj

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

07/17/08 00:44:13 changed by DD32

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 />';

07/18/08 04:28:09 changed by slambert

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.

07/18/08 04:33:07 changed by slambert

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.

07/18/08 22:05:01 changed by ryan

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.

07/20/08 21:58:27 changed by rejon

  • 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...

07/20/08 22:00:20 changed by rejon

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?

07/20/08 22:06:40 changed by rejon

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...

07/20/08 22:07:31 changed by rejon

  • cc set to jon@rejon.org.

07/20/08 22:18:55 changed by rejon

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...

07/20/08 22:26:52 changed by rejon

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

07/21/08 00:19:40 changed by jacobsantos

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

07/21/08 00:33:32 changed by jacobsantos

  • keywords set to needs-testcases.

07/21/08 00:36:04 changed by jacobsantos

  • keywords changed from needs-testcases to needs-unit-tests.

08/06/08 22:27:31 changed by ryan

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.

08/07/08 07:23:39 changed by pyadav

  • owner changed from anonymous to pyadav.
  • status changed from new to assigned.

08/16/08 20:51:40 changed by westi

  • milestone changed from 2.6.1 to 2.6.2.

2.6.1 has been released, moving to 2.6.2 milestone

08/19/08 21:27:58 changed by ryan

  • attachment get_posts_debug.diff added.

Dump get_posts() query object.

08/19/08 21:29:39 changed by ryan

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.

08/21/08 14:22:19 changed by tandilboy

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; ?>

08/21/08 14:22:54 changed by tandilboy

  • attachment Query_result.txt added.

query result

08/21/08 15:52:21 changed by ryan

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().

08/22/08 12:36:08 changed by tandilboy

  • attachment WP_Query_PluginDisabled.txt added.

WP Query -Plugin Disabled-

08/22/08 12:37:05 changed by tandilboy

i disabled the plugin check the new file

08/22/08 17:18:34 changed by ryan

That looks better. Does it work?

08/22/08 18:14:31 changed by tandilboy

disabling the plugin isnt works....

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

08/22/08 19:06:35 changed by ryan

Are the posts in that category published?

08/22/08 20:47:52 changed by tandilboy

I CHECK THE ERROR....

is a plugin error U_U

sorry

08/22/08 20:55:42 changed by tandilboy

i using that plugin

http://wordpress.org/extend/plugins/front-page-excluded-categories/

any fix or workaround?

08/22/08 21:21:48 changed by ryan

  • attachment 7326.diff added.

Suppress filters for get_posts()

08/22/08 21:22:56 changed by ryan

Try 7326.diff.

08/28/08 22:30:28 changed by ryan

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

08/29/08 10:46:48 changed by DD32

(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)

08/29/08 10:48:35 changed by DD32

  • attachment 7326.2.diff added.

08/29/08 10:50:03 changed by DD32

attachment 7326.2.diff added.

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

08/29/08 18:33:05 changed by ryan

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

09/01/08 14:39:55 changed by tandilboy

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

09/01/08 17:01:47 changed by ryan

  • status changed from assigned to closed.
  • resolution set to fixed.

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