[wp-hackers] get_posts bug, or feature?
andrew rader
andrew.r.rader at gmail.com
Sun Dec 9 18:21:27 GMT 2007
Ok, here's two queries that looked to be in the right section:
string(359) "SELECT DISTINCT * FROM wp_posts ,
wp_term_relationships, wp_term_taxonomy WHERE 1=1 AND post_type =
'post' AND post_status = 'publish' AND (wp_posts.ID =
wp_term_relationships.object_id AND
wp_term_relationships.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id AND wp_term_taxonomy.term_id = 10)
GROUP BY wp_posts.ID ORDER BY post_date DESC LIMIT 0,5"
string(284) "SELECT t.*, tt.*, tr.object_id FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN
wp_term_relationships AS tr ON tr.term_taxonomy_id =
tt.term_taxonomy_id WHERE tt.taxonomy IN ('category', 'post_tag') AND
tr.object_id IN (18) ORDER BY t.name ASC"
so it looks like the first one is trying to select those with cat =
10. I looked at the mysql tables, and I think I found the culprit:
mysql> select * from wp_term_relationships where object_id=18;
+-----------+------------------+
| object_id | term_taxonomy_id |
+-----------+------------------+
| 18 | 10 |
| 18 | 11 |
+-----------+------------------+
The object_id=18 is because that's the post id that's giving me
problems. So, somehow adding a post to category 11 made it also be
added to cat 10... I should note that all of the UI shows the post
only being in category 11 (for example, on the admin page when I edit
the post it shows only the one category)
Could this be because I originally had the post in category 10, but
decided it was better suited for the sub-category and moved it?
perhaps there's a bug in changing a post's categories?
On Dec 6, 2007 10:25 AM, Otto <otto at ottodestruct.com> wrote:
> Add define('SAVEQUERIES', true); to your wp-config.php file.
>
> Then somewhere in your theme, you can dump the queries to the output.
> Like add this to the footer.php file:
> <?php echo "<!--\n"; var_dump($wpdb->queries); echo "\n-->"; ?>
>
> Then load the page showing the problem, view source, and all the SQL
> you ran will be at the bottom of the page.
>
> Remove the SAVEQUERIES afterwards, it's a bit of a performance hit to
> leave it on.
>
>
>
> On 12/6/07, andrew rader <andrew.r.rader at gmail.com> wrote:
> > yes, I am using get_posts:
> >
> > $p = get_posts('category=10'); echo $p[0]->post_title;
> >
> > I have no idea how get_posts is doing this, but it definitely is. How
> > can I turn on query logging?
> >
> > On Dec 6, 2007 9:51 AM, Otto <otto at ottodestruct.com> wrote:
> > > Okay, I'm confused. Are you using get_posts or not?
> > >
> > > >From looking at the code, I don't think get_posts will return children.
> > > However, query_posts will indeed do just that.
> > >
> > > So your links with cat=11 and cat=10 will indeed behave the way you're
> > > saying, but I can't see how get_posts() will do what you're talking
> > > about. If you say it is, then it is, but I can't see how it could.
> > >
> > > Maybe if you turned on query logging and logged the actual SQL that
> > > occurs on your get_posts() call, we could figure it out.
> > >
> > >
> > >
> > > On 12/6/07, andrew rader <andrew.r.rader at gmail.com> wrote:
> > > > sorry for the delay, I just got around to verifying this.
> > > >
> > > > Okay, so here's my layout:
> > > >
> > > > 3 total categories, with IDs 9, 10, and 11. 9 and 10 are siblings, and
> > > > 11 is a sub category to 10. Category 10 has no posts, and category 11
> > > > has 1 post. if I do get_posts('category=11'), I see that 1 post. if I
> > > > do get_posts('category=10') I still see that one post. This is clearly
> > > > wrong.
> > > >
> > > > This also happens if I view the categories: ie, if I browse to
> > > > site.com/?cat=11 I see the 1 post like I'm supposed, but if I go to
> > > > site.com/?cat=10 I still see that post.
> > > >
> > > > this seems like a bug to me, if I wanted sub categories I'd ask for them.
> > > >
> > > > On Dec 4, 2007 1:44 PM, Otto <otto at ottodestruct.com> wrote:
> > > > > The get_posts function in post.php doesn't look like it will do what
> > > > > you say it's doing. It doesn't merge children into the category part
> > > > > of the query. It's adding this to the where clause
> > > > >
> > > > > AND (wp_posts.ID = wp_term_relationships.object_id AND
> > > > > wp_term_relationships.term_taxonomy_id =
> > > > > wp_term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id =
> > > > > $category)
> > > > >
> > > > > That's not going to get children categories too.
> > > > >
> > > > > Are you use that you're not using $wp_query->get_posts() instead?
> > > > > Because that behaves differently.
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > On 12/2/07, andrew rader <andrew.r.rader at gmail.com> wrote:
> > > > > > Hi all,
> > > > > >
> > > > > > I'm revisiting a plugin I've written using WP 2.3.1, and I'm seeing
> > > > > > undesired behavior in get_posts.
> > > > > >
> > > > > > my plugin, fancy-categories, displays the posts in a given category
> > > > > > directly in the sidebar list. I use
> > > > > > get_posts("numberposts=0&category=$catID") to get the posts of a given
> > > > > > category. The problem is that this code returns not only posts in the
> > > > > > $catID category, but also posts in the categories that are SUB
> > > > > > categories to $catID.
> > > > > >
> > > > > > the documentation here:
> > > > > > http://codex.wordpress.org/Template_Tags/get_posts says that:
> > > > > >
> > > > > > category
> > > > > > (integer) Only show posts from this category ID. There is no default.
> > > > > >
> > > > > > This makes me think that get_posts should only return those posts
> > > > > > directly in that category. I also believe this is how it worked prior
> > > > > > to 2.3 (but I have yet to verify)
> > > > > >
> > > > > > is this a bug? or a "feature". If it's intended behavior, I think the
> > > > > > get_posts open should allow you to exclude posts in sub-categories
> > > > > >
> > > > > > thanks,
> > > > > > Andy
> > > > > > _______________________________________________
> > > > > > wp-hackers mailing list
> > > > > > wp-hackers at lists.automattic.com
> > > > > > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > > > > >
> > > > > _______________________________________________
> > > > > wp-hackers mailing list
> > > > > wp-hackers at lists.automattic.com
> > > > > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > > > >
> > > > _______________________________________________
> > > > wp-hackers mailing list
> > > > wp-hackers at lists.automattic.com
> > > > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > > >
> > > _______________________________________________
> > > wp-hackers mailing list
> > > wp-hackers at lists.automattic.com
> > > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > >
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list