WordPress多站点主站$post变量为空及查询无结果求助
Hey there, let's work through this WordPress multisite main site content issue — I’ve dealt with similar headaches with directory-based multisites before, so let’s break this down step by step.
First: Rule Out the Obvious
- Check if your main site actually has content: Log into your main site’s admin (
mySite/wp-admin) and head to Posts → All Posts or Pages → All Pages. If there are no published posts/pages here, that’s why your queries are returning empty! If you intended the main site to pull global content, skip to the "Pulling Global Content" section below. - Verify your Reading Settings: Go to Settings → Reading in the main site admin. Make sure "Your homepage displays" is set to either "Your latest posts" (with published posts existing) or a published page. Choosing a deleted/unpublished page will leave the homepage empty.
Check Multisite Configuration & Query Restrictions
1. Confirm wp-config.php Multisite Settings
Double-check your wp-config.php to ensure the main site’s ID is correctly defined (main site should always be BLOG_ID_CURRENT_SITE = 1):
define('WP_ALLOW_MULTISITE', true); define('MULTISITE', true); define('SUBDOMAIN_INSTALL', false); // Critical for directory-based multisites define('DOMAIN_CURRENT_SITE', 'your-domain.com'); define('PATH_CURRENT_SITE', '/'); define('SITE_ID_CURRENT_SITE', 1); define('BLOG_ID_CURRENT_SITE', 1);
If these values are off, WordPress might be routing queries to the wrong site database tables.
2. Check for Theme/Plugin Query Tampering
Since all sites share the same theme, it’s possible a custom hook is accidentally restricting the main site’s queries. Look in your theme’s functions.php (or any custom plugins) for hooks like pre_get_posts or posts_where that might target the main site.
For example, this is a common mistake that would blank the main site:
add_action('pre_get_posts', 'restrict_posts_to_subsites'); function restrict_posts_to_subsites($query) { if (!is_admin() && $query->is_main_query()) { // Oops — this blocks all main site queries if (get_current_blog_id() === 1) { $query->set('post__in', array(0)); } } }
If you find code like this, adjust it to either remove the main site restriction or modify it to fit your global content needs.
3. Test for Plugin Conflicts
Temporarily deactivate all plugins, then check if the main site pulls content. If it works, reactivate plugins one by one to find which one is causing the issue. Some multisite-specific plugins add query restrictions that might accidentally target the main site.
Database Table Checks
Your main site’s content lives in the wp_posts table (assuming your database prefix is wp_). Log into your database tool (like phpMyAdmin) and:
- Confirm
wp_postshas rows withpost_status = 'publish'and validpost_typevalues (e.g.,post,page). - If the table looks empty or corrupted, restore from a backup or use the "Repair Table" option in phpMyAdmin.
Pulling Global Content (Across All Sites)
If your goal is to show content from your en and de subsites on the main site, the default WP_Query and get_posts() will only pull from the main site’s database table. To fetch content across sites, use switch_to_blog() to iterate through each subsite:
// Get all subsites $all_sites = get_sites(); $global_content = array(); foreach ($all_sites as $site) { // Switch to the subsite switch_to_blog($site->blog_id); // Fetch posts (adjust args as needed) $subsite_posts = get_posts(array( 'numberposts' => 5, 'post_status' => 'publish' )); // Merge into global content array $global_content = array_merge($global_content, $subsite_posts); // Switch back to main site restore_current_blog(); } // Now you can loop through $global_content to display posts foreach ($global_content as $post) { setup_postdata($post); // Display post content (e.g., the_title(), the_content()) } wp_reset_postdata();
Just make sure to always call restore_current_blog() after switch_to_blog() to avoid breaking subsequent queries.
内容的提问来源于stack exchange,提问作者Gergely Vízvári

