WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 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 6 years ago.
5317_for_24.diff (1.4 KB) - added by MichaelH 6 years ago.
for trunk 2.4 bleeding
missing-parameter-6490.diff (1.4 KB) - added by flinkflonk 6 years ago.
Missed parameter in patchset 6490 fixed.

Download all attachments as: .zip

Change History (15)

comment:1 Kafkaesqui6 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') ";

comment:2 Kafkaesqui6 years ago

  • Milestone changed from 2.4 to 2.3.2

comment:3 Kafkaesqui6 years ago

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

comment:4 lloydbudd6 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.

MichaelH6 years ago

comment:5 MichaelH6 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.

MichaelH6 years ago

for trunk 2.4 bleeding

comment:6 ryan6 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

comment:7 MichaelH6 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.

comment:8 ryan6 years ago

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

flinkflonk6 years ago

Missed parameter in patchset 6490 fixed.

comment:9 alakhnor6 years ago

Still not available in release 2.3.3

comment:10 ryan6 years ago

  • Milestone changed from 2.5 to 2.3.3

comment:11 ryan6 years ago

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

comment:12 DD326 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.