Make WordPress Core

Opened 17 years ago

Closed 15 years ago

#4943 closed enhancement (invalid)

Add features to get_posts: select from multiple categories, or exclude categories

Reported by: johnjosephbachir's profile johnjosephbachir Owned by:
Milestone: Priority: low
Severity: minor Version:
Component: Template Keywords:
Focuses: Cc:

Description

This patch adds two features to get_posts(), while maintain backward compatibility with all previous invocations:

  • The category variable may be a comma-separated list, specifying selection of posts from multiple categories.
    get_posts('numberposts=5&category=1,5,6'); // get the 5 most recent posts from categories 1,5, and 6 (singular still works as before)
    
  • The query may be inverted, so that the results are from any categories BUT the ones specified.
    get_posts('numberposts=5&category=3,4&category_exclude=1'); // get the 5 most recent posts from all categories other than categories 3 and 4 (singular also works)
    

I haven't thoroughly tested this code, it's more for discussion purposes at this point. But I'm fairly confident that it's a complete solution.

(btw-- there is no 2.2.3 yet in the trac version menu)

Attachments (1)

get_posts_category_list_and_exclusion.diff (2.0 KB) - added by johnjosephbachir 17 years ago.

Download all attachments as: .zip

Change History (8)

#1 @johnjosephbachir
17 years ago

  • Version set to 2.2.3

#2 @DD32
17 years ago

The underlying code for categories has changed in 2.3
It appears that 2.3 allready has the ability to exclude/include posts based on the category

See line 199/235 here:
http://trac.wordpress.org/browser/trunk/wp-includes/post.php#L193

#3 @DD32
17 years ago

Scratch that, Sorry, It only includes/excludes based upon the Post ID's, not the categories.

But you may want to create patches/modifications based on the current 2.3(trunk) source rather than the 2.2 code branch.

#4 @santosj
16 years ago

  • Component changed from General to Template

#5 @ffemtcj
16 years ago

  • Milestone changed from 2.5 to 2.6

#6 @Denis-de-Bernardy
15 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from 2.9 to Future Release
  • Priority changed from normal to low
  • Severity changed from normal to minor

#7 @filosofo
15 years ago

  • Keywords needs-patch removed
  • Milestone Future Release deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version 2.2.3 deleted

For a while now get_posts has been based on WP_Query, so you can use all the typical Loop parameters: categoryin, showposts, etc.

Note: See TracTickets for help on using tickets.