You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress多站点主站$post变量为空及查询无结果求助

Troubleshooting Empty Content on Your WordPress Multisite Main Site

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_posts has rows with post_status = 'publish' and valid post_type values (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:05:19