Switch_to_blog() outside wordpress
This may not be possible. From the Codex page Integrating WordPress with your Website, which explains how to use WordPress “outside” of WordPress: These directions will not work on a MultiSite Network.
This may not be possible. From the Codex page Integrating WordPress with your Website, which explains how to use WordPress “outside” of WordPress: These directions will not work on a MultiSite Network.
You might be running up against WordPress’s global $blog_id variable in test case #2. Try this: foreach($sites as $my_blog_id => $name) { switch_to_blog($my_blog_id); $sites[$my_blog_id] = get_bloginfo(‘name’); restore_current_blog(); }
No, it’s retrieving the posts just fine, but you’re not storing them anywhere. Here’s your code again, with line-by-line documentation: // Set up global variables. Great global $wpdb, $blog_id, $post; // Get a list of blogs in your multisite network $blog_ids = $wpdb->get_col( “SELECT blog_id FROM $wpdb->blogs” ); // Iterate through your list of blogs … Read more
If you just need the posts of each WP_Query object, you can do do something like this: <?php // fetch blogs here $all_posts = array() foreach($blogs as $b) { switch_to_blog($b->blog_id); $query = new WP_Query(/* your args here */); if($query->have_posts()) $all_posts = array_merge($all_posts, $query->posts); // posts are stored in the `posts` property restore_current_blog(); // FYI: this … Read more
You should first read (emphasis mine): The sites in a multisite network are separate, very like the separate blogs at WordPress.com. They are not interconnected like things in other kinds of networks (even though plugins can create various kinds of interconnections between the sites). If you plan on creating sites that are strongly interconnected, that … Read more
Since wordpress multisite uses different tables for all blogs, it is very inefficient to get all recent articles of all blogs (content aggregation) on display time since you have to query all blogs, sort the articles by date and display the ammount you need. Plugins like WordPress Post Indexer (https://premium.wpmudev.org/project/post-indexer) additional write all posts into … Read more
You could hook into the action switch_blog. You get the new blog ID as the first argument here. But loading the complete translation files here is expensive, you also have to restore the old files after that. WordPress does not use native gettext functions, but some custom code that is much slower. See #17268. The … Read more
Unfortunately, the switch_to_blog() method isn’t going to work for this purpose. switch_to_blog() is really only a partial switch – it makes some modifications to $wpdb that help with database queries. But it’s not a complete switch in the way you might imagine. In particular, dynamic_sidebar() depends on global called $wp_registered_sidebars. This global is populated by … Read more
@G.M.’s idea to cache the check has lead me to the following helper function. I’ve put it into the global namespace to have it available everywhere. The function doesn’t say anything about the blog status, just if it exists and is not marked as deleted. The database query is very fast (0.0001 seconds) and runs … Read more