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

WordPress中get_posts()在Ultimate Member用户资料页仅返回自定义文章类型部分正确数据的问题排查

Hey there, let's break down what's going on here and how to fix it—your hunch about loop variable conflicts is totally on the mark.

The Root of the Problem

The Ultimate Member (UM) user profile page runs in its own unique context where the global $post variable is likely repurposed to hold user-related data (not a standard WordPress post object). When you call setup_postdata($f), you override that global $post, but some template tags like the_title() get filtered or modified by UM's code to prioritize user info instead of your custom post.

Even worse, functions like the_tags() or the_excerpt() depend on the global post context being clean and unaltered. Since the profile page isn't a standard post archive, these tags don't behave as expected—even with setup_postdata() in place. The good news is your print_r($f) confirms all the post data is there; you just need to access it correctly.

Fixes to Try

Option 1: Use Post Object Properties Directly (Most Reliable)

Skip template tags that rely on the global $post and access data straight from your $f post object. This avoids all conflicts with UM's context:

$args = array( 
    'author' => $uid, 
    'numberposts' => 10, 
    'post_type' => 'ff', 
);
$ff = get_posts($args);

if($ff){
    foreach($ff as $f){
        // Content (apply WordPress filters manually to match the_content() behavior)
        echo apply_filters('the_content', $f->post_content);
        
        // Title (direct property access, no UM interference)
        echo $f->post_title;
        
        // Tags (use get_the_tag_list with your post's ID)
        echo get_the_tag_list('', ', ', '', $f->ID);
        
        // Excerpt (apply excerpt filters manually)
        echo apply_filters('the_excerpt', $f->post_excerpt);
        
        // Post date (use get_the_date with your post object)
        echo get_the_date('', $f);
    }
    // No wp_reset_postdata needed here—we never touched the global $post
}

Option 2: Manually Manage the Global Post Context

If you really want to use template tags like the_title() or the_tags(), explicitly save and restore the original global $post (since wp_reset_postdata() only resets to the main query, which is the user profile, not your custom posts):

$args = array( 
    'author' => $uid, 
    'numberposts' => 10, 
    'post_type' => 'ff', 
);
$ff = get_posts($args);

if($ff){
    // Save UM's original global post context
    global $post;
    $original_post = $post;
    
    foreach($ff as $f){
        $post = $f; // Manually set global post to your custom post
        setup_postdata($post);
        
        // Now template tags should work as expected
        the_title();
        the_content();
        the_tags();
        the_excerpt();
        the_time();
    }
    
    // Critical: Restore UM's original context so the profile page doesn't break
    $post = $original_post;
    wp_reset_postdata();
}

Why These Work

  • Option 1 bypasses the global $post entirely, so there's no chance of conflicting with UM's code. Applying filters manually ensures you still get the same formatting as using standard template tags.
  • Option 2 takes full control of the global $post during your loop, then puts everything back exactly as it was. This prevents unintended side effects on the rest of the profile page.

内容的提问来源于stack exchange,提问作者Hitoris

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 10:09:07