Wordpress,查询后更改了全局帖子

quo*_*web 1 php wordpress

我正在开发一个插件。在这个插件中,我创建了一个自定义帖子类型“toto”。

在管理页面中,我使用以下帖子 ID '82' 编辑自定义帖子元素。在这个页面中,我启动了一个查询来检索具有另一个 post_type 的元素:

$featured_args = array(
'post_type' => 'other_type',
'post_status' => 'publish'    
);

// The Featured Posts query.
$featured = new WP_Query( $featured_args );

// Proceed only if published posts with thumbnails exist
if ( $featured->have_posts() ) {
    while ( $featured->have_posts() ) {
        $featured->the_post();
        if ( has_post_thumbnail( $featured->post->ID ) ) {
            /// do stuff here
        }
    }

// Reset the post data
wp_reset_postdata();
}
Run Code Online (Sandbox Code Playgroud)

这样做全局 $post 改变了。它不再是 id 为 82 的帖子,而是来自查询的最新帖子元素。

我认为 wp_reset_postdata() 函数将允许我检索我当前的 $post。我也试过 wp_reset_query() 没有改变。我错过了什么吗?

任何帮助,将不胜感激。

小智 5

wp_reset_postdata()重置主循环。如果你想$post直接访问试试

global $post;
$backup_post = $post;

//do another loop

wp_reset_postdata();

$post = $backup_post;
Run Code Online (Sandbox Code Playgroud)