最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp reset postdata - wp_reset_postdata not working

programmeradmin0浏览0评论

i have this query

add_action('edit_form_after_title', 'get_books');

function get_books($main_post){
    global $post;
    $list = '';

    // print the current post (everything is still fine here)
    print_r($post);

    $args = array(
        'posts_per_page' => '20',
        'post_type'      => array('books')
    );
    $my_query = new WP_Query($args);
    if ($my_query->have_posts()) {
        $list .= '<select name="books_list">';
        while ($my_query->have_posts()) {
            $my_query->the_post();
            // my code
            $list .= '<option value="'.get_the_ID().'">'.get_the_title().'</option>';
        }
        $list .= '</select>';
        wp_reset_postdata();
    }
    // i try another methods but still same problem.
    wp_reset_postdata();
    $my_query->reset_postdata();
    wp_reset_query();


    // here print the last post in $my_query, not old post or empty post in add new post page
    print_r($post);

    echo $list;
}

but after complete the query he print last post in $my_query

my problem not in print_r exactly (only to show simple explain) but my problem on add new post or edit post page, he show featured image and other infomations of my last post in $my_query

i fix this problem by put $post in another variable before my query and restore it to $post after complete, but i want to know what problem with wp_reset_postdata or what misake i'm making.

发布评论

评论列表(0)

  1. 暂无评论