Make WordPress Core

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)

#1 follow-up: @jdgrimes
9 years ago

It looks like this is because in parse_query(), is_category is only set if the $tax_query has an 'operator' specified:

<?php
                        $this->parse_tax_query( $qv );

                        foreach ( $this->tax_query->queries as $tax_query ) {
                                if ( ! is_array( $tax_query ) ) {
                                        continue;
                                }

                                if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
                                        switch ( $tax_query['taxonomy'] ) {
                                                case 'category':
                                                        $this->is_category = true;
                                                        break;
                                                case 'post_tag':
                                                        $this->is_tag = true;
                                                        break;
                                                default:
                                                        $this->is_tax = true;
                                        }
                                }
                        }

And in parse_tax_query(), the 'cat' query arg doesn't end up using an operator:

<?php
                // Category stuff
                if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
                        $cat_in = $cat_not_in = array();

                        // snip

                        foreach ( $cat_array as $cat ) {
                                if ( $cat > 0 )
                                        $cat_in[] = $cat;
                                elseif ( $cat < 0 )
                                        $cat_not_in[] = abs( $cat );
                        }

                        if ( ! empty( $cat_in ) ) {
                                $tax_query[] = array(
                                        'taxonomy' => 'category',
                                        'terms' => $cat_in,
                                        'field' => 'term_id',
                                        'include_children' => true
                                );
                        }

                        // snip
                }

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.

#2 @swissspidy
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 @petersplugins
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 @vedantgandhi28
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 @r1k0
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-tests because this behaviour seems intentional.
  • adding reporter-feedback to let the reporter address @vedantgandhi28's comment.
Note: See TracTickets for help on using tickets.