簡體   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