admin管理员组文章数量:1312800
I have a father blog / main blog inside a multisite that has a specific taxonomy with 20+ terms inside it. i just need a list of all the terms inside that taxonomy...
i dont mind quering using WPDB but just dont know to how to write it correctly or in any other way i tried use: "switch_to_blog" but that doesnt work when quering terms
Any suggestion?
i tried the: Multisite Global Terms plugin (doesnt work)...
EDIT (response to question in comment)
this is an example of switch_to_blog i tried:
function multisite_profession_select(){
switch_to_blog(1);
$taxonomies = array('rsitecat');
$args = array('hide_empty' => false);
$terms = get_terms($taxonomies, $args );
echo '<pre>';
print_r($terms);
echo '</pre>';
restore_current_blog();
}
The reponse i get:
WP_ERROR OBJECT
(
[ERRORS] => ARRAY
(
[INVALID_TAXONOMY] => ARRAY
(
[0] => INVALID TAXONOMY
)
)
[ERROR_DATA] => ARRAY
(
)
)
I have a father blog / main blog inside a multisite that has a specific taxonomy with 20+ terms inside it. i just need a list of all the terms inside that taxonomy...
i dont mind quering using WPDB but just dont know to how to write it correctly or in any other way i tried use: "switch_to_blog" but that doesnt work when quering terms
Any suggestion?
i tried the: Multisite Global Terms plugin (doesnt work)...
EDIT (response to question in comment)
this is an example of switch_to_blog i tried:
function multisite_profession_select(){
switch_to_blog(1);
$taxonomies = array('rsitecat');
$args = array('hide_empty' => false);
$terms = get_terms($taxonomies, $args );
echo '<pre>';
print_r($terms);
echo '</pre>';
restore_current_blog();
}
The reponse i get:
WP_ERROR OBJECT
(
[ERRORS] => ARRAY
(
[INVALID_TAXONOMY] => ARRAY
(
[0] => INVALID TAXONOMY
)
)
[ERROR_DATA] => ARRAY
(
)
)
Share
Improve this question
edited May 21, 2015 at 22:34
Sagive
asked May 21, 2015 at 20:39
SagiveSagive
2,7822 gold badges35 silver badges54 bronze badges
7
|
Show 2 more comments
3 Answers
Reset to default 7 +50No way to do that in a "word press" way...
function multisite_profession_select(){
switch_to_blog(1);
$taxonomies = array('rsitecat');
$check_later = array();
global $wp_taxonomies;
foreach($taxonomies as $taxonomy){
if (isset($wp_taxonomies[$taxonomy])){
$check_later[$taxonomy] = false;
} else {
$wp_taxonomies[$taxonomy] = true;
$check_later[$taxonomy] = true;
}
}
$args = array('hide_empty' => false);
$terms = get_terms($taxonomies, $args );
echo '<pre>';
print_r($terms);
echo '</pre>';
if (isset($check_later))
foreach($check_later as $taxonomy => $unset)
if ($unset == true)
unset($wp_taxonomies[$taxonomy]);
restore_current_blog();
}
WP will check if Taxonomy exists, but on multisite blog, its only switching $table_prefix but not actually switching to blog. In this fixed code - it should trick WP to think that taxonomy registred and retrive a term from a mu blog tables. after request - you killing fake taxonomy ghosts.
P/S/
I didn't test code.
If you have problems getting data from another blog, polyfixes like temporarily registering taxonomies, etc. are a real pita in the long term.
You can't really rely on that workarounds, since such workarounds mimic internal wp infrastructure that changes over time.
Best practice imho would be to register a rest api endpoint (https://developer.wordpress/rest-api/extending-the-rest-api/adding-custom-endpoints/) or an xmlrpc endpoint (https://codex.wordpress/XML-RPC_Extending). The endpoint fetches all data you need and returns it as json. So you always have a clean local context of the blog queried and don't have to struggle with missing infrastructure.
Would be something like this in the source blog:
add_action(
'rest_api_init',
function () {
register_rest_route('your-namespace', '/get-terms/(?P<taxonomy>\d+)', array(
'methods' => 'GET',
'callback' => function($data) {
$taxonomy_slug = $data['taxonomy'];
return get_terms($taxonomy_slug, array('hide_empty' => false));
}
));
}
);
And something like that in the requesting blog:
$api_request = 'http://the.domain.of.your.blog/wp-json/your-namespace/get-terms/slug-of-taxonomy';
$api_response = wp_remote_get($api_request);
$taxonomy_terms = json_decode(wp_remote_retrieve_body($api_response), true);
Hope, that helps and best regards from Salzburg!
This is another way to solve, with wpdb object:
function multisite_profession_select(){
global $wpdb;
switch_to_blog(1);
$taxonomies = array('rsitecat');
$taxonomies = implode( "','", $taxonomies );
$terms = $wpdb->get_results( "
SELECT t.*
FROM
{$wpdb->prefix}term_taxonomy AS tt
INNER JOIN
{$wpdb->prefix}terms AS t
ON t.term_id = tt.term_id
WHERE
tt.taxonomy IN ( '{$taxonomies}' )
" );
echo '<pre>';
print_r($terms);
echo '</pre>';
restore_current_blog();
}
本文标签: Get all terms inside a specific taxonomy in a multisite
版权声明:本文标题:Get all terms inside a specific taxonomy in a multisite 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741916411a2404760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
switch_to_blog()
? In general case it should work... – Rarst Commented May 21, 2015 at 21:09