WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 20 months ago

#10908 closed enhancement (duplicate)

Impossible to exclude categries in a category listing

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

Description

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 4 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 dimitryz5 years ago

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

comment:2 dd325 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?

comment:3 dimitryz5 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.

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

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

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

scribu4 years ago

comment:7 scribu4 years ago

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

comment:8 nacin4 years ago

  • Milestone changed from 3.0 to Future Release

comment:9 upyup3 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' ";

into

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

the filter works as a charm

comment:10 scribu3 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.

comment:11 wonderboymusic20 months 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 20 months ago by wonderboymusic (previous) (diff)

comment:12 SergeyBiryukov20 months ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:13 SergeyBiryukov20 months ago

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