#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 has-patch |
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)
Change History (45)
#3
@
16 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
@
16 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
@
16 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.
#6
@
16 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
@
16 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
@
16 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...
#9
@
16 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
@
16 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...
#12
@
16 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
@
16 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
@
16 years ago
Write test cases for how you think the get_posts() function should work like.
#17
@
16 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.
#19
@
16 years ago
- Milestone changed from 2.6.1 to 2.6.2
2.6.1 has been released, moving to 2.6.2 milestone
#20
@
16 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
@
16 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; ?>
#22
@
16 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().
#25
@
16 years ago
disabling the plugin isnt works....
check the second WP-Query (the plugin disabled query) and please try to fix :(
#28
@
16 years ago
i using that plugin
http://wordpress.org/extend/plugins/front-page-excluded-categories/
any fix or workaround?
#32
@
16 years ago
attachment 7326.2.diff added.
Lines #781 & #837 are unrelated to the issue and fix undefined variable notices.
This ticket was mentioned in PR #7341 on WordPress/wordpress-develop by @jonsurrell.
3 months ago
#38
- Keywords has-patch added
## WORK IN PROGRESS
Implement a method to create a fragment parser from a tag in an HTML API processor.
Intended for use in #7326
Trac ticket:
@jonsurrell commented on PR #7341:
3 months ago
#39
Replaced by https://github.com/dmsnell/wordpress-develop/pull/22, this was intended to target #7326 as its base branch.
Possibly due to #6772