WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#20541 closed defect (bug) (invalid)

taxonomy_exists doesn't work within switch_to_blog

Reported by: mooontes Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.3.1
Component: Taxonomy Keywords:
Focuses: Cc:

Description

And because of this, wp_get_object_terms doesn't work either

The problem is that "taxonomy_exists" relies on "$wp_taxomies" array that is not repopulated when using "switch_to_blog".

Example:

Blog ID 1 -> taxonomies: 'companies', 'cities'

Blog ID 2 -> taxonomies: 'brands'

//We are at Blog 1

switch_to_blog(2);
$brands = wp_get_post_terms($post->ID, 'brands');

//This give us an error of "invalid taxonomy" (it's using blog 1 taxonomies' to check against)

Change History (6)

comment:1 @scribu3 years ago

This is also true for post_type_exists() and in general for all functions that rely on globals.

I guess being able to use switch_to_blog() would be one benefit of storing post type and taxonomy definitions in the database.

comment:2 follow-up: @dd323 years ago

The problem is that "taxonomy_exists" relies on "$wp_taxomies" array that is not repopulated when using "switch_to_blog".

switch_to_blog() simply switches to using that blogs database tables and details, it doesn't load the plugins that are activated for that site (nor can it), so any taxonomies registered by plugins on those site won't be available on the current blog.

I guess being able to use switch_to_blog() would be one benefit of storing post type and taxonomy definitions in the database.

But not having the plugin code responsible for those post types/taxonomies loaded in the current request, works against that advantage

As a result, I don't think there's anything we can actually do here and provide a reliable environment.. Storing it in the database is ok for an incredibly simple taxonomy, but for those, perhaps it should be registered for all the sites instead.

comment:3 in reply to: ↑ 2 @scribu3 years ago

Reading the bug report again, can you register a taxonomy network wide?

comment:4 @dd323 years ago

can you register a taxonomy network wide?

If it's registered in a plugin thats activated network wide, I don't see why not..

Last edited 3 years ago by dd32 (previous) (diff)

comment:5 @JustinSainton3 years ago

  • Cc justinsainton@… added

Running into this on a current project - experimenting with some different hacks - but it would be awesome to be able to rely on the taxonomy API in an environment where you've switch_to_blog()'d.

comment:6 @nacin3 years ago

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

If you have a taxonomy registered on the original blog, it will be available on the other sites you switch to.

switch_to_blog() is unfortunately not magical, and cannot load plugins and deactivate others based on the context of the site. By necessity, this makes many switch() situations depend on the architecture of the network.

Note: See TracTickets for help on using tickets.