0

I'm writing a query that switches to the main blog of a MU site using switch_to_blog(). All is well except i'm attempting to write a wp_query that includes a variable needed from the previous blog. All the content being pulled from the main blog is stored in a CPT with a taxonomy that corresponds to the various sub-blog names. I'm attempting to store bloginfo('name'); from my previous blog, and pull it into the query after switch_to_blog, but for obvious reasons, no luck (it gets the current bloginfo, not the previous.) Is there any way either to save the the previous bloginfo into a global of some sort, or to switch back to the previous blog just to get that variable mid-query?

See my query below for better illustration:

$location = bloginfo('name'); 
   switch_to_blog( 1 );

    $the_query = new WP_Query(  array(
        'post_type' => 'staff', 
        'posts_per_page' => -1, 
        'tax_query' => array( 
            array(
                'taxonomy' => 'location',  // name of tax
                'field'    => 'slug',
                'terms'    => $location, // this doesn't work, returns nothing
            ),
        ),
    ) ); ?>

  <?php if ( $the_query->have_posts() ) : ?>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php endwhile; ?>

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

<?php   restore_current_blog(); ?>

2 Answers 2

0

Your code, which I assume is in a function, should pass variables needed from the current blog. So, assuming that your code in your question is called by somefunction(), change that to pass along variables from the current blog, as in

// variables from the current blog
$current_blog_1 = "some value"
$current_blog_2 = "another value"
// do the code in the original question
somefunction ($current_blog_1, $current_blog_2);

It might even be more efficient to put current_blog values in an array, as in

// set some values before you call your code via a function
$current_blog_array = array();
$current_blog_array[] = "some value";
$current_blog_array[] = "another value";
// call the function with an array, empty if needed
somefunction ($current_blog_array = array());

Then process the array values in your original code.

0

bloginfo() echos, not returns, so it doesn't work to use it to save into the $location variable. Use get_bloginfo() instead and it should work to get the info from the site that calls your code.

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.