Opened 13 months ago

Closed 9 months ago

#20541 closed defect (bug) (invalid)

taxonomy_exists doesn't work within switch_to_blog

Reported by: mooontes Owned by:
Priority: normal Milestone:
Component: Taxonomy Version: 3.3.1
Severity: normal Keywords:
Cc: justinsainton@…

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)

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: ↓ 3   dd3213 months 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   scribu13 months ago

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

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 13 months ago by dd32 (previous) (diff)
  • 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.

  • 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.