switch_to_blog changes values of global variables (as you can see here: https://core.trac.wordpress.org/browser/tags/5.0.3/src/wp-includes/ms-blogs.php#L801). It doesn’t matter if you call it in function or not – these variables will get changed.
So yes – you always have to call restore_current_blog when you’ve done all you wanted with the switched blog.
Otherwise these variables will stay changed (so wpdb will query wrong tables and so on)
Related Posts:
- Get menu object from theme_location
- Prevent “main” WPMU site_url() being returned in functions
- Implementing DNS Prefetching with WordPress
- How to include one class/instance without using global variables
- syntax for remove_filter in parent theme with class
- Different functions file for each site in multisite installation?
- Delete pages and Create default pages for all new network sites
- Multisite, either change the name of sample-page or create new page
- Loop (iterate) through Multi-Site sub-sites (blogs)?
- Global functions on WPMU
- Run function when WordPress new multisite is created or ACF field is updated
- Conditional Gravity Forms filter in WordPress Multisite
- Multisite Ajax serialize return error [object Object]
- Creating new blog on multisite programatically without hacks?
- How to get correct URLs in network wide menu (Multisite)?
- Multisite 404 on pages – rewrite error breaks database
- Has is_archived() for sites in a multisite changed with WP 4.7?
- Function that outputs second theme url
- dynamic site link for future migration in echo do_shortcode()
- wp_get_attachment_image_src returns image on main domain, false on subdomain
- functions.php return custom text
- Dashboard blocker on network multisite for specific user roles
- Members only function, but allow account activation [solved]
- Child theme, how to cut the functions.php file into several classes?
- Pass an argument into a function to extract from array
- Competing Login Redirects – Need to be Combined?
- Adding a class to the body of an inactive site using multisite
- Run function from parent on child site
- How to save custom made object in an array in a post meta field
- Ads Only at one website inside multisite network – code error
- Different ads on every website on multisite environment
- How can I modify or filter this variable in an existing class? (Mai Theme)
- How to redirect to post if search results only returns one post
- Hard Code Pages into a Theme for a Network (multisite) Installation
- Overriding core functions in child theme
- Displaying a random user with a shortcode
- Assign category using custom field?
- Should I ask a theme developer to use locate_template rather than require_once
- Customized wp_new_user_notification
- Updating users_can_register option?
- How to add an anonymous function in wordpress hooks by removing create_function
- Changing post category from dropdown
- How to update feed only 2-3 times a week (for Feedburner email)?
- Fatal error when trying to get admin email on contact form
- Is it good practice to use wpdb->query() function?
- How to properly add NPM packages and integrate them in WordPress?
- wp_enqueue_script add integrity parameter
- change in form-checkout.php by using code in functions.php
- How to get the post_name when in preview?
- “is_new_day()” alternative for years?
- How can I make a widget shortcode to control all the widgets?
- admin-ajax.php + load-scripts.php hanging for minutes
- Unique sequential reservation code
- Accessing two databases wordpress
- Add a jQuery Function
- How do I change a parent theme’s function through the child theme?
- Enqueue script on every page except one
- Best way to handle lack of titles in microblogging
- List all blogs, but exclude the main site
- Redirect to other page if login
- 404 when enqueue_script using plugin_url
- WP Customizer API loaded into functions.php
- How to use get_posts() function in functions.php
- the_widget() inside functions.php
- How Can I add a menu to the theme from withen the function.php file
- How to hide content on mobile in the functions.php file – woocommerce
- Load templates, pass arguments, and render output from functions.php
- WordPress Lite/Light
- Is_Page doesnt detect my page
- Show image after X para – center aligned
- Problems adding a new field to product in cart
- function replace_text for entire page
- Create html from function
- wp_list_pages change of children and anchor of parent
- Admin Panel errors because of functions.php
- WordPress not performing direct update
- Using Output from one Function and calling it into another
- How to add a class and title attribute to the link generated by next/previous post
- “Headers already sent” while trying to add a CSS file to my login page?
- Notice: Use of undefined constant REQUEST_URI – assumed ‘REQUEST_URI’ in ….functions.php on line 73
- multible shortcodes (for differnt values) with one function
- Problem with lists and “allowed_block_types” in functions.php (since WP 6.1.1)
- HELP: Code To Check Status And Write Debug Entry
- How to enqueue Bootstrap 4.6 js & css from local files
- Template Redirect Function Only Working When Logged In
- Auto updating JavaScript dependancy in functions.php
- Swap WordPress “Widget Area” based on Page Template
- Wanted to get rid of Paragrapgh tag in submit buttom of the WordPress form
- Create section wit form
- wp_script_add_data not working
- CF7 Remove Comma from ‘select’ ‘radio’ and ‘checkbox’ outouts
- Timeline from diferent wordpress api urls endpoint works, but diferent date
- list all gutenberg blocks properties with icon name as well
- Child theme’s functions.php not being read (child theme’s autoload.php not being run)
- How to use: WP_AJAX_GET_COMMENTS
- wp_insert_post inserts 3 posts at once?
- Uploading images via ACF update_field function
- Help with adding fullcalendar.io to a WordPress page
- Add class to functions.php custom code to call font awesome icon
- is there a list of actions on WordPress