Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

get_post_meta fails after switch_to_blog

reading through your code …

if ( is_wp_error($change_ok) ) {
    WriteErrorLog($change_ok->get_error_message());
}

is not applicable as $change_ok will always be true. ref https://codex.wordpress.org/Function_Reference/switch_to_blog

as the comments state it is better to use get_posts() and this may fix your issue, otherwise use $wpdb->prepare before your get_results.

also a comment is that your SQL statement will not work for the primary site..
$sql_posts = "select id, post_title, post_content from wp_".$blog_id."_posts where post_type="custom_add" and post_status="publish"";

A note is that the “wp_” table prefix can be changed in the wp-config.php file and if a site has done the “wp_” hardcoded prefix would never work the $wpdb->base_prefix can be used to make the “wp_” always fit the current site.
also the wp_".$blog_id."_posts is not going to work for the main site as the table structure is different

a simple function to get the current site WP database table prefix…

// Get the database prefix
function get_blog_prefix( $blog_id=null ) {

    global $wpdb;

    if ( null === $blog_id ) {
        $blog_id = $wpdb->blogid;
    }
    $blog_id = (int) $blog_id;

    if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) {
        return $wpdb->base_prefix;
    } else {
        return $wpdb->base_prefix . $blog_id . '_';
    }
}

Related Posts:

  1. WPDB Multiple site’s posts and get featured images
  2. Update post meta using save_post hook in multisite
  3. WordPress MS wp-admin/includes/post.php error with shortcode generator
  4. How can meta values from another site in a multisite be used in a custom query?
  5. How to use WordPress Multisite With Different Domain Names?
  6. How To Add Custom Form Fields To The User Profile Page?
  7. Where are available Roles Defined in the wp_ database?
  8. restore_current_blog() vs switch_to_blog()
  9. Can i merge 2 new WP_Query($variable) ‘s?
  10. How to disable a network enabled plugin for just one site?
  11. How to reliably flush rewrite rules on multisite?
  12. Site Redirecting to wp-signup.php
  13. Moving a WP Multisite to a subdirectory
  14. Cookies in multisite where network sites have their own domain name
  15. Convert a Multisite WordPress install into a Single site
  16. WordPress MultiSite Active Directory integration and site privacy
  17. WordPress Job Interview Preparation
  18. How to add Custom Blog Options to new blog setup form?
  19. How can I trust switch_to_blog()?
  20. Remove the blog slug from WordPress Multisite root node
  21. Why are my roles not visible in a Multi-site/Network?
  22. Site admin in a network install can’t edit users?
  23. How to use same email for multiple users
  24. How to run an activation function when plugin is network activated on multisite?
  25. Specific upload folder for PDFs in custom Post type in WP multisite
  26. What is the correct way to map multiple domains in a WordPress 4.1 multisite install?
  27. Where can I find documentation on what characters are allowed in user names and why?
  28. How to migrate subsite from dev multisite to production multisite
  29. How can I un-reserve a pending username registration?
  30. How do I remove/delete an old super admin user?
  31. DB connection error after copying a WordPress Multisite instance to a second location
  32. Redirect Main Site to Subsite in Multisite WordPress
  33. Is there a is_user_logged_in() for multisite?
  34. Switching MultiSite installation from HTTP to HTTPS
  35. Allow user to select a theme to install when they signup
  36. Setting up a wordpress network with third level domains
  37. Get upload URL by blog ID in multisite
  38. What’s the difference between get_site_option and get_blog_option?
  39. Need help sorting “My Sites” Alphabetically
  40. How to delete post revisions?
  41. Create mobile site with same content just different theme
  42. Add additional Network constants to wp-config.php dynamically
  43. Multisite stuck at 1MB for max file size
  44. How to get blog name, when using WordPress Multisite
  45. What are options are there to implement a multi language site
  46. Switching primary site in WordPress Multisite
  47. Multisite Network Port Num Issues?
  48. Convert existing site to network
  49. Where is the robots.txt stored for a WordPress Multisite install?
  50. Settings API in Multisite – Missing update message
  51. Add WordPress MU Network Admin via Database
  52. How to get current site id? (WPMU)
  53. Multisite on Windows with wildcard subdomains
  54. Why is WordPress Multisite redirecting to wp-signup.php and how to fix it?
  55. Which asset URLs are acceptable in a “vanilla” MU install?
  56. $wpdb->base_prefix not get parent site prefix in multisite
  57. How to tell which database tables belongs to which multisite site
  58. what are pmxi tables? [closed]
  59. Using wp_insert_post() with Networking enabled
  60. Are nested subdomains possible with a subdomain multisite?
  61. Sharing Dynamic Sidebars across Multisite Blogs
  62. I want to develop locally with Multisite, then migrate each site to separate remote WP installs
  63. Password Reset for Users on a Multisite Subsite
  64. Anyone figured out how to use WordPress MultiSite with Domain Mapping & SSL?
  65. Can a multisite’s blogs be accessible from two different sub-domains?
  66. Querying posts from multiple sites in a network?
  67. Plugin for editing options on multisite?
  68. What do I need to do to convert my MultiSite from HTTP to HTTPS?
  69. is_front_page() malfunction?
  70. WP Multisite development with Mamp Pro and wildcard subdomains, not really working for me
  71. How to share WordPress core library
  72. How do I change the Multisite URL?
  73. Create site programmatically for WPMU
  74. Can upload doc and pdf but not ppt – not permitted for security reasons
  75. How Can I Centralize Content Usage and Approval in a WordPress Multi-Site installation
  76. WordPress Multisite: Adding actions to only one site
  77. WordPress Multisite allow site admin to add user without email confirmation
  78. Domain mapping (without plugin) in 4.5.x multisite?
  79. wordpress multisite, how to keep user on subdomain throughout registration process?
  80. How will self-hosted WordPress work with Google Page Speed Service?
  81. WordPress Multisite, NGINX and WordPress Subdirectory Install
  82. Multisite Domain Mapping with SSL
  83. get_user_meta() to Return User Meta Only for Current Blog in Multi Site
  84. Basic Multisite Question – Managing Content Centrally
  85. Can I set some default pages to be created on every creation of a new blog
  86. Customizing wp-activate.php
  87. Listing of all uploaded files in network sites
  88. Troubleshooting a “You do not have sufficient permissions to access this page” error
  89. Network not displaying all sites and users
  90. WordPress Multisite – When a user signs-up on main site, how to add the user to a subsite
  91. create /blog subsite on WordPress multisite
  92. multisite 404 error for subdirectory
  93. How to hide or remove unwanted widgets on Multisite installation?
  94. WordPress Multisite Multiple child theme
  95. Use wp_nav_menu() to display a Menu from another site in a Network install?
  96. How to Control user registrations directly on sub-sites
  97. multisite: use a virtual subfolder for subblogs
  98. How to set an option for all sites in my network?
  99. How to set up wordpress domain mapping on MAMP Pro
  100. How to hide admin account in BuddyPress? (for security reasons)
Categories multisite Tags multisite, post-meta
Profile page for user roles
How To Show All Custom Post Types In A Category Instead Of Pagination?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress