0

I trying get the categories in a function from a selected blog on my multisite with switch_to_blog, but I get the categories of the current blog, and I dont understand, why... I searched the solution on the net, but I dont find anything about this problem... Here is my code:

function getcatsfromblog(){
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
$blog_id = get_blog_id_from_url( 'site2.network.com' ); 
// GET CATS
switch_to_blog( $blog_id );
$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC'
) );

foreach( $categories as $category ) {
echo '<input type="radio" value="' . $category->name . '">' . $category->name . '</input>';
}
restore_current_blog();
}
getcatsfromblog();

Any idea, whats wrong with this? (I call this function with AJAX, but except of the swith_to_blog, everything working...)

1 Answer 1

0

The problem originate from the global value $blog_id. In similar case need to use anything else instead of $blog_id, like $my_blog_id. https://wordpress.stackexchange.com/a/166734/134567

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.