I have a multisite that has a custom post type called 'directory'. When I look through my subsites and ask each one to spit out the custom post types I can see it. My code is below.

 $sites = wp_get_sites();
        foreach ( $sites as $site ) {
            echo get_blog_details($site['blog_id'])->blogname."</br>";
            switch_to_blog( $site->blog_id );
               /// I am now in the site itself////

              $args = array(
                 'public'   => true,
                 '_builtin' => false // Use false to return only custom post types

             $post_types = get_post_types( $args );
             print_r($post_types); //THIS RETURNS 'directory'/////

Knowing that I have a post type called 'directory I finish this loop with a query to retrieve all posts of post_type='directory'.

 $args = array( 
               'post_type' => 'directory',
              $the_query = new WP_Query( $args );
       if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                    <h1 class="the-title"><?php the_title() ;?></h1>
                   <?php endwhile; else: ?> <p>Sorry, there are no posts to display</p> <?php endif;

This returns me the Sorry there are no posts to display when I try my directory variable as post type. If I try just post_type="post" it returns only the FIRST post from each site. Something is amiss here and I can't seem to figure it out. Why is my 'directory' post_type not querying? Why is the loop only showing the first post when it does return something? My first time dabbling in switch_to_blog.... Thanks.


1 Answer 1


That's not how switch_to_blog works.

When you load WP, it loads the theme and plugins into the global namespace, with no sandboxing or walls. When you then call switch_to_blog, there's no way to put all of that to one side and load the plugins/theme from the other blog.

What switch_to_blog will do though is switch the database tables used and the data context for stuff. It can't reload the theme/plugins, and since post types and taxonomies are defined in code, and not the database, they won't change.

Think of it like pretending to be me, that doesn't mean you are now the same height/gender/weight as me, likewise switch_to_blog just changes the context/data, it doesn't reload the code.

This means only the theme and plugins of the site the request was made to will be loaded, you can't call functions or reference post types that only exist on the other blog. As a result get_post_types will always return the same thing by design.

The only reliable way you have of querying which post types another site has, or running code in that sites context with all the plugins, is to make a HTTP request, or spawn a WP CLI process from PHP.

Eitherway this approach is a dead end, not worth pursuing, and rests upon a flawed assumption of how WP loads code.

To fix this you have several options:

  • register the custom post type on both sites, not just the blog you're switching to, it only needs the register_post_type call, nothing else
  • use the REST API to grab the data, this would mean a new request but that new request would be loaded with the code of the other site
  • Thanks for the input Tom. So I am pulling data external to WP in a JSON pull. All my sites in the multisite have the directory CPT. I was trying to loop through each site in the multisite and insert this JSON data in the CPT 'directory'. With how flexible the WP Query functions are I thought I could find a way to just loop through each site from one script to do what I want. It looks like maybe I could run a cron that fires each script associated with a sub to do what I want. You are correct, this might be a flawed assumption on my part. Thanks.
    – jeynon
    Oct 25, 2019 at 19:29
  • "This function is useful if you need to pull posts, or other information, from other blogs." So this is why I thought switch blog might work.
    – jeynon
    Oct 25, 2019 at 19:42

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.