WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#32526 closed defect (bug) (duplicate)

Tax_Query doesn't working in switch_to_blog()

Reported by: phill_brown Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2.2
Component: Networks and Sites Keywords:
Focuses: multisite Cc:

Description (last modified by ocean90)

Scenario:

  • Site 1 has registered taxonomy called news_category. Site 2 hasn't.
  • Site 2 runs switch_to_blog(1);
  • Site 2 runs WP_Query with a tax_query parameter

eg

$news = new WP_Query([
    'post_type' => 'news',
    'tax_query' => [
        [
            'taxonomy' => 'news_category',
            'field' => 'slug',
            'terms' => 'wordpress',
        ]
    ]
]);

The above query returns 0 results because the tax_query portion errors and fails the WHERE clause. In this case, it's caused when the clean_query method fails a call to taxonomy_exists(). Taxonomy_exists is also baked into the logic of other subsequent functions in that method such as is_taxonomy_hierarchical and get_term_children.

This looks to be a far-reaching issue related to a ticket raised and closed years ago: #20541.

I've attached a patch which may resolve the issue specific to Tax_Query.

Attachments (1)

taxonomy.diff (503 bytes) - added by phill_brown 4 years ago.

Download all attachments as: .zip

Change History (3)

@phill_brown
4 years ago

#1 @ocean90
4 years ago

  • Description modified (diff)

#2 @jeremyfelt
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Thanks for the ticket and patch, @phill_brown. I don't think there's much we can do here. The attached patch solves a specific scenario, but would not make all tax queries work in a switched context.

The taxonomy is available in that it's stored in the database, but to properly use it as part of the query, any plugin registering that taxonomy should also be activated on the querying site.

At its core, this ticket is a duplicate of #20541 due to the taxonomy_exists() requirement in taxonomy queries.

Note: See TracTickets for help on using tickets.