wp_reset_postdate未按预期重置

问题描述

我有一个wordpress功能,可在我的网站的两页,主页和类别存档中使用。尽管此代码可以在类别存档中完美运行,但首页似乎存在wp_reset_postdata()问题-但我不知道如何解决。尝试过wp_reset_query(),但似乎没有什么不同。

下面的这段代码输出特定类别中的帖子列表(对于本示例,我只是输出ID)。我有一个单独的查询,用于检查帖子是否已“固定”。如果有,它将嵌套在查询的位置1(这样可以有效地将帖子输入到数组输出中)

if ( $wpb_all_query->have_posts() ) :

                while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
                    if( $count === 1 ) {
                        if ( $pinnedID ) {
                            while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post();
                                echo $count . ": " . get_the_ID();
                                echo "<br/>";
                            endwhile;
                            wp_reset_postdata();
                                echo $count . ": " . get_the_ID();
                                echo "<br/>";
                        }
                        else {
                            echo $count . ": " . get_the_ID();
                            echo "<br/>";
                        }
                    }
                    else{
                        if($pinnedID === get_the_ID()){
                        }
                        else{
                            if ( $pinnedID ) {
                                if( $count === 0 || $count === 8 ) {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                } 
                                else {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                }
                            }
                            else{
                                if( $count === 0 || $count === 9 ) {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                } 
                                else {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                }   
                            }
                        }   
                    }                   
                    $count ++;
                endwhile;
                wp_reset_postdata();
endif;

如果没有“固定”帖子,则输出以下内容

0: 13402
1: 13383
2: 13409
3: 13397
4: 13361
5: 13332
6: 10886
7: 10884
8: 10862
9: 10795

如果有“固定”的帖子,则输出

0: 13402
1: 10619
1: 171 <- this is the ID of the homepage - it should be ID #13383
2: 13409
3: 13397
4: 13361
5: 13332
6: 10886
7: 10884
8: 10862
9: 10795

我知道这与wp_reset_postdata()有关-但我不知道如何解决。有人有什么想法吗?

解决方法

我已经设法弄清楚了这个

if ( $pinnedID ) {
    $backup=$post;
    $wpb_pinned_content_query = new WP_Query($innerargs);
    while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post();
        echo $count . ": " . get_the_ID();
        echo "<br/>";
    endwhile;
    $post=$backup;
    if($pinnedID !== get_the_ID()) {
        echo $count . ": " . get_the_ID();
        echo "<br/>";
    }

}

通过内联声明辅助查询,然后将全局$ post存储到备份变量中-我能够再次访问原始$ post,而无需调用wp_reset_postdata();