wp重置查询无法在admin_notices

时间:2016-06-08 07:03:59

标签: php wordpress

我正在编写插件,我需要显示有关帖子的管理员通知。我打电话给提供消息的功能。

add_action('admin_notices', array($this, 'postNotices'));

这是我的功能:

public function postNotices() {

    $args = array(
        'post_type' => 'demo_post_type',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'ignore_sticky_posts' => 1
    );
    $query_post = new WP_Query($args);

    if ($query_post->have_posts()) {
        while ($query_post->have_posts()) {
            $query_post->the_post();
            // $my_meta = 'Im getting post meta here'; ?>
                <div>Some notification</div>
            <?php wp_reset_query();
        }
    }
}

显示的通知工作正常但是当我尝试创建新帖子时,它会显示最后发布的数据,例如新帖子页面中的帖子标题。 这意味着我认为wp_reset_query()函数不能与admin_notices钩子一起使用。有人提出建议吗?

任何建议都非常感谢。

1 个答案:

答案 0 :(得分:0)

经过艰苦的研究,我得到了答案, 我们走了,

     $args = array(
        'post_type' => 'demo_post_type',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'ignore_sticky_posts' => 1
    );

    $query_posts = get_posts($args);
    foreach ($query_posts as $post) {
        ?>
         <div>Some notification</div>
        <?php
    }

无需添加wp_reset_query()等功能。找到答案但不知道逻辑是什么。如果有人知道,请在这里发表评论。