latest 5 posts using switch_to_blog loop

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

Merging multiple wp_query objects

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

Get Recent Posts by Date in Multisite

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

switch_to_blog(): Load textdomain

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

Sharing Dynamic Sidebars across Multisite Blogs

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

How can I trust switch_to_blog()?

@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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)