Opened 14 years ago
Closed 12 years ago
#15959 closed defect (bug) (fixed)
get_adjacent_post() can generate invalid SQL
Reported by: | dd32 | Owned by: | westi |
---|---|---|---|
Milestone: | 3.5 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Query | Keywords: | has-patch needs-testing |
Focuses: | Cc: |
Description
As reported on wp-testers, if get_adjacent_post() is used with $in_same_cat = true
on a post type which does not support categories, then a invalid SQL will be generated.
eg:
SELECT p.* FROM wp_posts AS p INNER JOIN wp_term_relationships AS tr ON p.ID = tr.object_id INNER JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category' AND tt.term_id IN () WHERE p.post_date < '2010-07-04 12:24:53' AND p.post_type = 'note' AND p.post_status = 'publish' AND tt.taxonomy = 'category' ORDER BY p.post_date DESC LIMIT 1
specifically
AND tt.term_id IN ()
Whilst this is not a problem in the 2011 theme, Some themes may call the functions which can result in this.
wp-testers thread: http://lists.automattic.com/pipermail/wp-testers/2010-December/013841.html
one potential solution would be:
// Check to see if the Post Type supports categories before honoring the argument. if ( $in_same_cat ) { $taxonomies = get_object_taxonomies($post); if ( ! in_array('category', $taxonomies) ) $in_same_cat = false; }
From what I can see, this would not be a regression from any previous version of WordPress
Attachments (5)
Change History (21)
#1
@
14 years ago
- Component changed from General to Template
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
- Version changed from 3.1 to 3.0
#2
@
14 years ago
- Keywords has-patch added; needs-patch removed
Made a patch from Dion's proposed solution.
#3
@
14 years ago
Related, but unrelated to the problem at hand - #13096 - use a custom taxonomy in place of category
#4
@
14 years ago
- Component changed from Template to Query
Just updated the patch. Simply check if the array of object terms is empty.
#7
@
13 years ago
This bug also manifests on post types which do support categories, but where no categories are set for a given post. Present in 3.2(18084).
Alternate patch attached which fixes both cases.
#11
@
12 years ago
- Milestone changed from Future Release to 3.5
This is pretty lame, we should fix it.
#12
@
12 years ago
- Keywords needs-testing added
Patch should catch scenarios where category taxonomy doesn't exist, post_type doesn't support the category taxonomy, and if post doesn't have any categories. We should return empty in all of these cases as we should honour the $in_same_cat
request.
#14
@
12 years ago
Slightly tweaked patch on #22360 — use of is_object_in_taxonomy() rather than the WP_Error check, I guess.
Just confirming this was an issue in 3.0 as well.