WordPress.org

Make WordPress Core

Opened 10 years ago

Last modified 4 months ago

#10663 new defect (bug)

Category name handling wrong function is_category

Reported by: Azaroth Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.8.4
Component: Query Keywords: has-patch
Focuses: Cc:
PR Number:

Description

When putting numbers in front of the category names, Wordpress uses them as basis for the function is_category which leads to problems with e.g. plugins using this function.
Example:

  • 3 Categories: "News" (id 1), "1st Release" (id 2), "2nd Release" (id 3)
  • Trying to check the category with is_category(1) works for news, but also returns true for the category "1st Release"
  • Trying to check the category with is_category(2) returns true for both, "1st Release" and "2nd Release"

This behaviour is not expected. It might be interesting when you explicitely give the category numbers as names (e.g. "1", "2", "3"). Changing the in_array function to a simple equal comparison makes everything work as expected.

Attachments (3)

10633.diff (786 bytes) - added by wojtek.szkutnik 9 years ago.
10663.diff (2.5 KB) - added by wonderboymusic 5 years ago.
10663.2.diff (2.6 KB) - added by wonderboymusic 5 years ago.

Download all attachments as: .zip

Change History (16)

#1 @scribu
10 years ago

  • Component changed from General to Template
  • Keywords needs-patch added
  • Milestone set to 2.9

#2 @azaozz
10 years ago

  • Milestone changed from 2.9 to Future Release

No patch.

#3 @scribu
10 years ago

Changing the in_array function to a simple equal comparison makes everything work as expected.

The problem is that you have to also be able to do this:

is_category(array(1, 2))

is_category(array('cat-slug-a', 'cat-slug-b'))

is_category(array('Cat Name A', 'Cat Name B'))

is_category(array(1, 'cat-slug-a', 'Cat Name A'))

Of course, this can cause all sorts of ambiguities.

#4 @scribu
10 years ago

  • Keywords dev-feedback added; needs-patch removed

#5 @azaozz
10 years ago

Perhaps the fix would be to do a strict comparison

in_array( $cat_obj->term_id, $category, true )

although checking for category ID and using a string would then fail: is_category(3) will work, is_category('3') will not.

#6 @wojtek.szkutnik
9 years ago

  • Cc wojtek.szkutnik@… added
  • Keywords has-patch needs-testing gsoc added

Correct me if I'm wrong, this should work :)

#7 @nacin
6 years ago

  • Component changed from Template to Query
  • Keywords close added

This feels like something we probably should not try to do. It's fairly ambiguous in nature. Even if we have a clear way of doing it internally, it's not exactly straightforward to use it. It's a cheap function, feels nicer to call it a few times.

@wonderboymusic
5 years ago

#8 follow-up: @wonderboymusic
5 years ago

  • Keywords needs-unit-tests added; dev-feedback needs-testing gsoc close removed
  • Milestone changed from Future Release to 4.0

10663.diff - this bug sucks, we should fix it. All of my new assertions pass except for $this->assertTrue( is_category( "$cat_id" ) ); - strangely does work for is_tag(), which we should also attack here.

#9 @wonderboymusic
5 years ago

  • Keywords commit added; needs-unit-tests removed

PHP is the worst

#10 @helen
5 years ago

Where are we with this? Do people actually want this committed, is_tag(), etc.?

This ticket was mentioned in IRC in #wordpress-dev by DrewAPicture. View the logs.


5 years ago

#12 in reply to: ↑ 8 @DrewAPicture
5 years ago

  • Keywords commit removed
  • Milestone changed from 4.0 to Future Release

Sounds like there's some stuff left to do here:

Replying to wonderboymusic:

10663.diff - this bug sucks, we should fix it. All of my new assertions pass except for $this->assertTrue( is_category( "$cat_id" ) ); - strangely does work for is_tag(), which we should also attack here.

Punting.

#13 @chriscct7
4 years ago

Patch is still good. @wonderboymusic did you want to continue pursuing this this?

Note: See TracTickets for help on using tickets.