Make WordPress Core

Opened 13 years ago

Closed 12 years ago

Last modified 9 years ago

#20541 closed defect (bug) (invalid)

taxonomy_exists doesn't work within switch_to_blog

Reported by: mooontes's profile 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 (9)

#1 @scribu
13 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.

#2 follow-up: @dd32
13 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.

#3 in reply to: ↑ 2 @scribu
13 years ago

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

#4 @dd32
13 years ago

can you register a taxonomy network wide?

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

Version 0, edited 13 years ago by dd32 (next)

#5 @JustinSainton
12 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.

#6 @nacin
12 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.

#7 @jeremyfelt
9 years ago

#32526 was marked as a duplicate.

#8 @dd32
9 years ago

#35231 was marked as a duplicate.

This ticket was mentioned in Slack in #core by sergey. View the logs.


9 years ago

Note: See TracTickets for help on using tickets.