WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#5317 closed defect (bug) (fixed)

get_posts returns invalid data when using category= parameter

Reported by: MichaelH Owned by:
Milestone: 2.5 Priority: normal
Severity: normal Version: 2.3.1
Component: General Keywords: has-patch
Focuses: Cc:

Description

Take a new 2.3.1 install with this code:

$myposts = get_posts('category=2');

You would expect to get NO results, as an out-of-the-box install has no posts with category 2, but that code will return the "Hello World!" post.

Guessing this might be caused by get_posts not discerning between post categories and link categories (e.g. the link with link_id=1 in category 2 causes post with ID=1 to be returned).

Attachments (3)

5317_for_231.diff (1.3 KB) - added by MichaelH 8 years ago.
5317_for_24.diff (1.4 KB) - added by MichaelH 8 years ago.
for trunk 2.4 bleeding
missing-parameter-6490.diff (1.4 KB) - added by flinkflonk 8 years ago.
Missed parameter in patchset 6490 fixed.

Download all attachments as: .zip

Change History (15)

#1 @Kafkaesqui
9 years ago

Line #257 in post.php:

$query .= empty( $category ) ? '' :
AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id
AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
AND $wpdb->term_taxonomy.term_id = " . $category. ") ";

This should be:

$query .= empty( $category ) ? '' :
"AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id
AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
AND $wpdb->term_taxonomy.term_id = " . $category. "
AND $wpdb->term_taxonomy.taxonomy = 'category') ";

#2 @Kafkaesqui
8 years ago

  • Milestone changed from 2.4 to 2.3.2

#3 @Kafkaesqui
8 years ago

Changed milestone to 2.3.2, since this is an easy fix to slip in.

#4 @lloydbudd
8 years ago

  • Milestone changed from 2.3.2 to 2.4

Kafkaesqui, thank you for your participation! Please don't change the milestones, leave that for the project leads.

#5 @MichaelH
8 years ago

  • Keywords has-patch added

+1 for getting this in 2.3.2 -- it's just one less problem to explain in the forums ;)

Patch attached.

@MichaelH
8 years ago

for trunk 2.4 bleeding

#6 @ryan
8 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [6490]) Fetch only post category terms in get_posts(). Props MichaelH. fixes #5317

#7 @MichaelH
8 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening because [6490] doesn't look to have the complete fix from the 5317_for_24.diff.

#8 @ryan
8 years ago

I moved everything in the prepare(), but I think I got it all.

@flinkflonk
8 years ago

Missed parameter in patchset 6490 fixed.

#9 @alakhnor
8 years ago

Still not available in release 2.3.3

#10 @ryan
8 years ago

  • Milestone changed from 2.5 to 2.3.3

#11 @ryan
8 years ago

[6646] fixes the missing cat from [6490]. I'll milestone this for 2.3.4, in case we do another release.

#12 @DD32
8 years ago

  • Milestone changed from 2.3.4 to 2.5
  • Resolution set to fixed
  • Status changed from reopened to closed

2.3.x is dead, 2.5 is stable released, Marking as fixed in 2.5

Note: See TracTickets for help on using tickets.