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 to delete post revisions?
  5. Upload path/URL weirdness for the main blog of a multisite/network setup
  6. WP_Site_Query vs. WP_Network_Query in WordPress 4.6
  7. Multisite/network–automatic self-registration, blog setup
  8. COOKIE_DOMAIN setting confusion
  9. Restrict users on multisite WordPress install
  10. Possible to make custom role in multisite that can add sites?
  11. Which is the recommended method to transfer a wordpress site to wordpress Multisite?
  12. Hierarchical Page Listing of all Pages in a Multisite Network
  13. Asian language multisite IDN subdomain
  14. Using separate Apache log files for multisite
  15. Nginx rules for subdomain multisite install (Bedrock)
  16. Changing Multisite URL after the subsite has been in use
  17. Can’t find “Add New” button for themes in a multisite
  18. Network setup tab is not shown under Tools
  19. Set multisite settings when new site created
  20. SQL to check how many sites a user is a member of
  21. Multi-site: Change the URL of main site to HTTPS
  22. How to show sites under “My Sites” Menu in admin panel wordpress multisite?
  23. What type of Google tracking should I use?
  24. Force Uploads Use Yearmonth to off (0) for all existing sites in Multisites
  25. How can I get multisite primary blog (url or path) for current user?
  26. convert from single site to multisite (network) with existing subdomains
  27. Replacing the default content created while the site creation
  28. Changing admin sub-domain in WordPress multisite
  29. Yoast Sitemap grouping external sitemaps [closed]
  30. Change Username?
  31. Multisite, can’t see sub blogs
  32. Google Docs Viewer have problem in MultiSite sub-blogs
  33. upgrade from WPMU 2.9.2 to WP 3.2.1
  34. What does “Use BalanceTags” Do?
  35. Images don’t show up
  36. How to create a network using a command line script?
  37. How to list WP multisite languages
  38. How can I automatically duplicate a site’s pages onto network site?
  39. How to fix blocked cookies error that doesn’t let me log into wp-admin?
  40. Draft preview and customize permission problems on multisite main site
  41. Gravity Forms: gform_wrapper stays hidden even though JS-scripts load without errors
  42. Multisite User cannot access dashboard
  43. Development of new theme and rollout on multisite
  44. WordPress Network (multisite) — how to ensure each site has their own admin URL?
  45. WordPress multisite proper prefix attribution
  46. Is it possible for a child-site of a multisite to be a multisite itself?
  47. WordPress stuck in deleting user
  48. Multisite installation on an existing single installation
  49. Multisite Sub domains not working
  50. How Can I Change The Name Of My Subdomain in WordPress Multisite
  51. Display site description instead of site name
  52. Error : Cookies are blocked or not supported by your browser
  53. WordPress multi-site: How do I create the home page, the root URL?
  54. Multiple Websites that share some content types and not others
  55. Solving a get_user_meta() problem in Multisite
  56. Is it possible to query posts with tax queries after multisite switch_to_blog() function?
  57. In Multisite the users profile picture keeps disappearing for “the other site”
  58. How to use the sunrise.php script for Multisite network domain mapping?
  59. How to pull data from child installation to parent theme
  60. Which is the correct way to migrate content from single WP to WP multisite
  61. Two installs conflicting – Pages redirecting
  62. Website not listed under Sites (in a Network environment)
  63. Running hundreds of WP in multisite
  64. Give users acces to admin a single post or set up WP network?
  65. WordPress Multisite for multi-language
  66. get_site_option / update_site_option – the main site and sub sites do not share the same storage
  67. Bitnami multisite change URL of secondary blog
  68. Can I use the main Multisite as a backend only?
  69. Number of total comment does not tally with the number of comment?
  70. Downloading customized theme from wordpress
  71. Domain not redirected correctly with WordPress Multisite
  72. Subsites in Multisite throw 404 in wp-admin
  73. WordPress theme to support single website but with 3 sections for 3 languages
  74. Can assign a new domain to a site in a multisite?
  75. WordPress multisite condition for certain blog_id’s
  76. the menu of the network does not display my second website
  77. Multisite – site user limited only for this site
  78. Update siteurl and home in multisite subsites to https
  79. WordPress multi-domain with multiple sites with multiple languages
  80. Get variable from previous blog while using switch_to_blog
  81. Domain level problem for multisite?
  82. Redirect www.mainsite.com/subsite/wp-login to www.mainsite.com/wp-login
  83. WordPress multisite not working
  84. Activation of new Registered site fails on multisite
  85. New multisite redirects to main site
  86. How to login a user with wp_set_auth_cookie on a specific blog within a multisite environment
  87. How to disable style filtering in post?
  88. How does adding custom meta to signup form work?
  89. Can’t un-archive site after upgrading to WordPress 3.8
  90. How to handle responsive and non responsive websites in one WordPress Theme?
  91. Create a Network of Different websites with wordpress
  92. WP Multisite: Do a function after blog is activated
  93. How to login Separately in each blog?
  94. Cookies in Multisite network where sites have their own domain name?
  95. Swap domains of two sites with each other in multisite
  96. Change a subsite Admin role of a WordPress Multisite after 24 hours registering
  97. different image for mobile template block theme
  98. MultiSite Setup
  99. Multisite: merging two subsites in the main site
  100. wp search-replace returns no output on Multisite installation
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
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 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