Opened 9 years ago
Last modified 4 weeks ago
#42164 new defect (bug)
Conditional Tags not working when using ugly URL
| Reported by: | petersplugins | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Query | Version: | 4.8.2 |
| Severity: | normal | Keywords: | needs-patch reporter-feedback |
| Cc: | Focuses: | template |
Description
When accessing a category archive via its pretty URL e.g.
example.com/category/whatever
the conditional tag
is_category()
returns true.
Accessing the same category archive via its ugly URL
example.com?cat=whatever
the conditional tag
is_category()
returns false.
I'm pretty sure this concerns other conditional tags too. This is not only a cosmetic flaw because it causes WP to use the wrong template. I've tested it with Twenty Seventeen and the homepage template was used in case of using an ugly URL.
Due tue the ugly URL always work independently of permalink settings this is a bug in my opinion.
Change History (5)
#2
@
9 years ago
- Keywords needs-patch needs-unit-tests needs-testing added
Should if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) be changed to if ( ! isset( $tax_query['operator'] ) || 'NOT IN' !== $tax_query['operator'] )? Seems like this would fix it.
#3
in reply to: ↑ 1
@
9 years ago
Replying to jdgrimes:
I don't know if this is expected behavior or not
I came across the same and this is exactly what I thought...
#4
@
8 weeks ago
Hi @petersplugins,
In the pretty URL, whatever is the category slug. In the ugly URL, cat only accepts a numeric category ID, not a slug, so ?cat=whatever gets treated as 0 and no category filter is applied at all, which is why it falls through to the homepage template. If you want to pass a slug on the ugly URL, use category_name instead, e.g. ?category_name=whatever - that goes through the same code path as the pretty URL and sets is_category() correctly.
Testing with a numeric ID (?cat=123) confirms it is working as intended, so this doesn't look like a bug in the operator handling flagged in the earlier comments. Can you please confirm with a numeric ID and update the ticket if the issue is still there.
#5
@
4 weeks ago
- Keywords reporter-feedback added; needs-unit-tests needs-testing removed
Thank you, @vedantgandhi28, for testing.
When a permalink isn't set, the cat parameter explicitly uses the term_id(an int). See here: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/taxonomy.php#L4874
Updating keywords:
- removing
need-testing&needs-unit-testsbecause this behaviour seems intentional. - adding
reporter-feedbackto let the reporter address @vedantgandhi28's comment.
It looks like this is because in
parse_query(),is_categoryis only set if the$tax_queryhas an'operator'specified:And in
parse_tax_query(), the'cat'query arg doesn't end up using an operator:I don't know if this is expected behavior or not. Looking at the history of the
isset( $tax_query['operator'] )condition might tell the tale.