繁体   English   中英

wp重置查询在admin_notices中不起作用

[英]wp reset query not working in admin_notices

我正在编写一个插件,我需要显示有关帖子的管理员通知。 我调用了提供消息的函数。

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挂钩一起使用。 有没有人建议?

任何建议都将受到高度赞赏。

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

     $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()类的函数。 找到了答案,但不知道逻辑是什么。 如果有人知道,请在这里评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM