the_post() within switch_to_blog() altering my excerpt

I have a strange problem I'm facing when using WP_Query with multisite in a plugin I'm writing which hooks into add_meta_boxes. I'm wanting to display all post titles in a dropdown in a meta box. While adding or editing a post in blog no. 2 (it could be any blog no.), I am looping through blog no. 1 and getting post titles from a custom post type to populate in a select dropdown. This works as it should, but in …
Category: Web

Get_posts not returning any posts when used with switch_to_blog

I'm attempting to pull in some order information from the "shop_order" post_type. This is on a multisite network, so I'm using switch_to_blog(1). When I set the post type as "post" it works great and I get the posts from the correct site. When I set the post_type to "shop_order" I get nothing, despite there being 20,000 'shop_order' records in the database. switch_to_blog(1); $args = array ( //'post_type' => 'post', 'post_type' => 'shop_order', 'posts_per_page' => 2, ); $posts = get_posts( $args …
Category: Web

How can I trust switch_to_blog()?

When I call switch_to_blog() with a blog id, I don’t know whether that blog actually exists. The function returns always true. Test case: switch_to_blog(PHP_INT_MAX); $post = get_post(1); restore_current_blog(); This will result in database errors that are exposed to the user. How can I prevent that? Real-world use case I was the lead developer of MultilingualPress. When a user translates a post, she gets a screen like this: Now the following can happen: She saves the post successfully and continues translating …
Category: Web

Not able to retrieve image id from sibling site within a switch_to_blog

Here is how I am trying to grab the image id for 'white_logo'. switch_to_blog($site->blog_id); echo attachment_url_to_postid( get_theme_mod('white_logo')); restore_current_blog(); The echo is printing '0'. It seems that when I use 'attachment_url_to_postid' for grabbing IDs of images that are within the current site's media library that it works. But when I try to grab IDs of images within other sites media libraries I'm not getting the desired response. Is there an alternative way to achieve this? Thank you! EDIT NOTE: I should …
Category: Web

switch_to_blog( ) content disappears after load

I'm working on building a custom extension to BuddyBoss that adds a tab to the user's profile to view that particular user's wishlist. When the page initially loads the correct data is being pulled in by the code but after 1-2 seconds the content that was pulled in from the shortcode disappears and shows no tractors in the user's wishlist. Is there a way to get this to stop? I am using a multi-site WordPress setup and the Favorites list …
Category: Web

switch_to_blog() + do_action('generate_footer') not working in multisite

I have a multisite installation and try to output the main site's header & footer on all subsites. In header it's working fine: switch_to_blog( '1' ); do_action( 'generate_header' ); restore_current_blog(); But in footer the switch itself is working, as it outputs the main site's name, but it doesn't show the footer - it outputs only empty widget-areas, neither the ones from the main-site nor the ones from the subsite. switch_to_blog( '1' ); echo get_bloginfo( 'name' ); do_action( 'generate_footer' ); restore_current_blog(); …
Category: Web

How to update global header and footer

I have a problem with the global header and footer in multisite. When I change the root's header and footer, the subsites (all other blogs) don't update. This is my code: global $switched; switch_to_blog(1); get_header(); restore_current_blog(); switch_to_blog(1); get_footer(); restore_current_blog();
Category: Web

Sort posts according to date

I have problem with sort posts according to date: My code now: <?php global $switched; $original_blog_id = get_current_blog_id(); // get current blog $blog_ids = array(4,1); foreach( $blog_ids as $blog_id ){ switch_to_blog( $blog_id ); $args = array( 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC'); $myposts = get_posts( $args ); foreach ( $myposts as $post ): setup_postdata( $post ); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; wp_reset_postdata(); switch_to_blog( $original_blog_id ); //switched back to current blog restore_current_blog(); …
Category: Web

switch_to_blog() and wp_nav_menu() not functioning properly

I have an MU plugin active on the network with this small function to query menu 4 from the main site in my network. On the main site in the network (1) things work as expected. On the sub sites in the network (2,3,4) no menu gets displayed (since I have the fallback set to false, and no menu is found). I'm wondering if I'm doing something wrong here, or if something larger is going wrong. /** * Multisite menu …
Category: Web

get_term_by works but get_terms doesn't?

I currently have a Multisite with 2 sites. From the secondary site get_term_by() works to retrieve terms from the primary site, but get_terms() does not. I'm using switch_to_blog(1); but I get object(WP_Error)#1316 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data"]=> array(0) { } } when I run get_terms() Is there a way around this?
Category: Web

Display posts from one network site on another

I've got to be missing something very basic here. I want to display posts from one site on another. No custom queries (yet), just the plain old posts. if ( get_current_blog_id() == 1 ) : // do regular main site stuff; elseif ( get_current_blog_id() == 6 ) : switch_to_blog( 1 ); // pull in posts from main blog if ( have_posts() ) : while ( have_posts() ) : the_post(); get_template_part( 'content/post' ); endwhile; endif; restore_current_blog(); else : // nothing special …
Category: Web

Register submenu on network menu from the subsite

I have a multisite like this: MainSite Site1 Site2 I want to register submenu page on active site in parent page /wp-admin/options-general.php on network in parent page /network/settings.php If I use below code snippet and activated from MainSite then it works. add_action( 'network_admin_menu', __CLASS__ . '::menu' ); add_action( 'admin_menu', __CLASS__ . '::menu' ); static public function menu() { add_submenu_page( 'options-general.php', 'TEST', 'TEST', 'delete_users', 'test-settings', __CLASS__ . '::render' ); } But, If I activate it from Site1 or Site2 then: It …
Category: Web

Multisite - Get current post id

I'm using WPMU in my project, but I'm facing some problems to get the current post id from all sites. If I run this code, I receive the same post id of current site and not the different post id from all other sites. It's like that switch_to_blog doesn't work. How can I get all ids of current post from all sites? $sites = get_sites(); /** @var WP_Site $site */ foreach ($sites as $site) { if ( $site->archived || $site->spam …
Category: Web

Switch_to_blog() outside wordpress

how can I load switch_to_blog() function outside wordpress? $blog_id = '7'; require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' ); switch_to_blog($blog_id); echo get_option('blogname'); restore_current_blog(); echo 'You switched back.'; I get error: Fatal error: Call to a member function set_prefix() on a non-object in /home/public_html/wp-includes/ms-settings.php on line 126 Any ideas ? UPDATE It can be loaded but without any function just this code ? What global variables is missing ?
Category: Web

How do I use a combination of switch_to_blog() and wc_create_order()?

I'm looking for a way to create a WC order programmatically for a subsite in my multisite environment. When I use the following, the order is created on the main site and not the subsite with ID 7: switch_to_blog( 7 ); // Now we create the order $bestelling = wc_create_order(); // Get orderid from order-to-be-made $neworderid = trim( str_replace( '#', '', $bestelling->get_order_number() ) ); // Get product data $occurences = $_POST['productids']; foreach($occurences as $key => $value) { // Add products …
Category: Web

restore_current_blog required after switch_to_blog, if I use that in a function?

class Basics { public function build_frontend_post_form( $blog_object ) { switch_to_blog( $blog_object->blog_id ); if( !( current_user_can( 'administrator' ) and current_user_can( 'publish_posts' ) ) ) { restore_current_blog(); return false; } restore_current_blog(); #DO STUFF } } In the exclusion (and anyway if not necessary for my logic) I need to use it restore_current_blog, or enough the return false? So In this case the base blog will be restoring after the function or not?
Category: Web

Include Site 1 Posts in Query for Sub Sites

So I have a Primary site that posts announcements about the sub sites, for each of those posts I have a meta value that the user sets for which site those announcements apply too. On each of those sub sites, I'd like to include those announcements for that site in the regular post loop. So far I've only been able to display them separately $latest_posts = get_posts( array( 'numberposts' => 5 ) ); switch_to_blog('1'); $networkPosts = get_posts( array( 'posts_per_page' => …
Category: Web

How to get all pages on specific blog after switch_to_blog?

I am attempting to loop through all sites on my multisite network blog. However, when I attempt to use get_pages it ignores the fact that the blog has switched via switch_to_blog. $sites = wp_get_sites( array( 'limit' => 1000 ) ); foreach ( $sites as $site ) { $blog_id = intval( $site['blog_id'] ); if ( $blog_id < 2 ) { continue; } switch_to_blog( $blog_id ); $pages = get_pages( array( 'sort_order' => 'asc', 'sort_column' => 'ID', 'post_type' => 'page', 'post_status' => 'publish', …
Category: Web

What is the way to share data between sites in a multisite

We want to display the same data on multiple sites in a multisite. This data needs to be added/edited by the client. We can create a custom post type and use the switch_to_blog function to get the right data on each site. But isn't there a better solution? Maybe a custom table where we save the data and get the data from. Or using the REST API to get the data from a blog or the custom table. What would …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.