Make WordPress Core

Opened 14 years ago

Closed 11 years ago

#10908 closed enhancement (duplicate)

Impossible to exclude categries in a category listing

Reported by: dimitryz's profile dimitryz Owned by: ryan's profile ryan
Milestone: Priority: normal
Severity: normal Version: 2.8
Component: Query Keywords: reporter-feedback
Focuses: Cc:


Setting the 'category__not_in' query_var in a WP_Query object does nothing if 'category_name' is also present. The consequence is that it's impossible to hide the posts of some child categories for a category listing.

The offending code is in line 1843:

$whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";

A dot (.) should be added before the equal (=) to indicate concatenation:

$whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";

The proposed solution enabled the addition of categories ('category__in') and exclusion of categories ('category__not_in') when the following condition is met:

if ( '' != $q['category_name'] && !$this->is_singular ) {

Attachments (1)

10908.diff (767 bytes) - added by scribu 13 years ago.

Download all attachments as: .zip

Change History (14)

#1 @dimitryz
14 years ago

  • Component changed from General to Query
  • Owner set to ryan

#2 @dd32
14 years ago

What would be the use-case of this?

category_name is asking to query for a specific item, category__not_in is saying, for the following generic query, i do not want the following items to be there.. When would they be used together?

#3 @dimitryz
14 years ago

When you're listing the posts for a category. Currently, all posts for that category and posts for that category's children are displayed. If you wanted to exclude the posts of one or more child categories, 'category__not_in' should do the trick.

#4 @scribu
14 years ago

You can use 'postsnot_in' instead, as I'm pretty sure allowing 'categorynot_in' together with 'category_name' will have hidden side effects.

#5 @dimitryz
14 years ago

I'm not sure about scribu's answer. Excluding posts is more trouble since those posts must first be retrieved. That may be a *lot* of posts.

I also don't understand that you mean by category_name. 'category__not_in' is used with an array of term_ids, not names.

#6 @scribu
13 years ago

  • Milestone changed from Unassigned to 3.0
  • Type changed from feature request to enhancement

I can see the use-case for this now.

Also see #12891

13 years ago

#7 @scribu
13 years ago

  • Keywords has-patch needs-testing added; category wp_query category__not_in removed

#8 @nacin
13 years ago

  • Milestone changed from 3.0 to Future Release

#9 @upyup
13 years ago

  • Cc upyup added
  • Keywords category wp_query category__not_in added; has-patch needs-testing removed
  • Type changed from enhancement to feature request
  • Version changed from 2.8.4 to 3.1

I have the same problem on v3.0.1

the goal being to set a user filter on specific categories (chosen in the plugin options page)

If the user chose the category 4 ("Adultes" categorie) and then goes to /category/adultes , he is presented the posts from this categories instead of the expected "No post found" page

When changing line 1938

$whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";


$whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";

the filter works as a charm

#10 @scribu
13 years ago

  • Keywords reporter-feedback added; category wp_query category__not_in removed
  • Type changed from feature request to enhancement
  • Version changed from 3.1 to 2.8

Please try it with version 3.1-alpha.

'Version' represents the earliest version in which the bug is reproducible.

#11 @wonderboymusic
11 years ago

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

This problem has been washed away by time. Pro tip: the string "$whichcat" no longer exists in the WordPress codebase.

Last edited 11 years ago by wonderboymusic (previous) (diff)

#12 @SergeyBiryukov
11 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

#13 @SergeyBiryukov
11 years ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.