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

WordPress中两次使用post__not_in仅一次生效的问题求助

问题解决:WordPress中两个post__not_in同时使用时第二个失效

问题现象

在functions.php中定义了两个使用post__not_in参数的WP_Query查询函数,单独调用任意一个都能正常排除当前文章,但同时调用时第二个函数的post__not_in会被忽略,其余功能正常。

核心原因

  1. wp_reset_postdata()位置错误:第一个函数wpb_postsbycategory2中,wp_reset_postdata()写在了return $string之后,导致该语句根本不会执行。第一个WP_Query的循环调用the_post()会修改全局$post对象,后续第二个函数调用get_the_ID()时,拿到的是第一个循环最后一篇文章的ID,而非当前页面的原始文章ID,所以post__not_in失效。
  2. 变量未初始化:两个函数中的$string变量未提前初始化,可能引发PHP警告,虽不影响功能但不符合规范。
  3. 未定义变量$post_id:第二个函数中调用get_the_post_thumbnail($post_id, ...)时,$post_id未定义,应使用get_the_ID()或$post->ID。

修复步骤

  • 将第一个函数中的wp_reset_postdata()移至return $string之前,确保每次查询后恢复全局$post对象。
  • 为两个函数的$string变量添加初始值$string = '';。
  • 替换第二个函数中未定义的$post_id为get_the_ID()。
  • 修正action中调用的短码名称,原代码中[SHORTCODE1]/[SHORTCODE2]应为实际注册的[categoryposts]/[categoryposts2]。

修改后的完整代码

function wpb_postsbycategory2() {
    $string = ''; // 初始化变量
    // 查询配置
    $the_query = new WP_Query( array( 
        'post__not_in' => array(get_the_ID()),
        'category_name' => 'news', 
        'posts_per_page' => 1
    ) ); 
    
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post(); 
            $string .= '<div class="block inline-group " data-widget-id="inline-group-0-no-images">
            <ul><li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li></ul></div></div>';
        }
    }
    
    /* 恢复原始文章数据 */
    wp_reset_postdata();
    return $string;
}
add_shortcode('categoryposts2', 'wpb_postsbycategory2');

add_action('inline_group_0', function () {
    echo do_shortcode('[categoryposts2]');
}, 10);


// 第二个查询函数
function wpb_postsbycategory() {
    $string = ''; // 初始化变量
    // 查询配置
    $the_query = new WP_Query( array( 
        'post__not_in' => array(get_the_ID()),
        'category_name' => 'news', 
        'posts_per_page' => 2
    ) ); 
    
    if ( $the_query->have_posts() ) {
        $string .= '<ul class="result-grid desktop-2 mobile-2">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            if ( has_post_thumbnail() ) {
                $string .= '<li><div class="recoinc">';
                // 替换未定义的$post_id为get_the_ID()
                $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail(get_the_ID(), array( 450, 300) ) . get_the_title() .'</a></li>';
            } else { 
                // 无特色图片时的输出
                $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
            }
        }
        $string .= '</ul>';
    }
    
    /* 恢复原始文章数据 */
    wp_reset_postdata();
    return $string;
}
add_shortcode('categoryposts', 'wpb_postsbycategory');

add_action('after_feed_block_0', function () {
    echo do_shortcode('[categoryposts]');
}, 20);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 19:23:19