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

wp query - Trouble with wp_reset_postdata() in Admin Panel

programmeradmin1浏览0评论

I'm hooking into 'edit_form_after_editor'

add_action('edit_form_after_editor', 'make_the_button_to_generate_a_report_number');
function make_the_button_to_generate_a_report_number($post) {

Check that I am dealing with a custom post type of 'publications'

if ($post->post_type != 'publications') return;

Create a number of arguments that I then run in a WP_Query

$new_query = new WP_Query($report_number_args);

use that data to do stuff...
Then I want to reset $post back to how it was, so I run

wp_reset_postdata();

But $post stays on the last item I had received from my new WP_Query().

What am I doing wrong? Is there a special way to reset the postdata when dealing with a custom post type?

Update: At the beginning and end of the function I have:

echo("<h4>the title = </h4>");
echo(the_title());

At the beginning the title is the title for the custom post type I clicked on to edit, or blank if I am creating a new post. At the end the title is always the last post from my query.

I'm hooking into 'edit_form_after_editor'

add_action('edit_form_after_editor', 'make_the_button_to_generate_a_report_number');
function make_the_button_to_generate_a_report_number($post) {

Check that I am dealing with a custom post type of 'publications'

if ($post->post_type != 'publications') return;

Create a number of arguments that I then run in a WP_Query

$new_query = new WP_Query($report_number_args);

use that data to do stuff...
Then I want to reset $post back to how it was, so I run

wp_reset_postdata();

But $post stays on the last item I had received from my new WP_Query().

What am I doing wrong? Is there a special way to reset the postdata when dealing with a custom post type?

Update: At the beginning and end of the function I have:

echo("<h4>the title = </h4>");
echo(the_title());

At the beginning the title is the title for the custom post type I clicked on to edit, or blank if I am creating a new post. At the end the title is always the last post from my query.

Share Improve this question edited Nov 3, 2020 at 16:40 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Nov 2, 2020 at 21:00 SoloCrowdSoloCrowd 571 silver badge7 bronze badges 2
  • Where are you calling wp_reset_postdata() in relation to the rest of the loop? – Howdy_McGee Commented Nov 2, 2020 at 21:27
  • I believe so, I call it right at the end of my function, outside of any loop that works with the $new_query data. Is there something I should look for to know it's being called in relation to the rest of the loop? – SoloCrowd Commented Nov 2, 2020 at 22:41
Add a comment  | 

1 Answer 1

Reset to default 3

I believe the official stance is to not use WP_Query in the admin panel. There'a a trac ticket #18408 on this exact same issue. Here's what Boone says:

The root issue is that wp_reset_postdata() is designed to reset globals to match the main query. But in the case of post.php, there is no main query - the $post global is populated manually, using get_post(), rather than through the normal $wp_query loop.

Because the post is populated manually in the admin panel you will not be able to reset it in a traditional fashion. If you print out the global $wp_query at this point you'll also find that it's empty.

The preferred method is to instead use get_posts() which doesn't override the global $post object.


You could manually preserve the global $post object in a custom variable and manually reset it once you're done with your loop. That being said, I do not know the overarching consequences with this method. It's safer to use get_posts() when possible.

global $post;

$tmp_post = $post;
$posts_query = new WP_Query( $args );

if( $posts_query->have_posts() ) {
    
    while( $posts_query->have_posts() ) {
        $posts_query->the_post();
        the_title();
    }
    
    $post = $tmp_post;
}

Another solution may be, specifically in the admin panel, store the global $post in the global $wp_query object during loop_start. For example:

/**
 * Store the global $post object so we may reset it
 * 
 * @return void
 */
function wpse377565_admin_post_storage() {
    
    global $wp_query,
        $post;
    
    if( ! is_admin() ) {
        return;
    }
    
    $wp_query->post = $post;
    
}
add_action( 'loop_start', 'wpse377565_admin_post_storage' );

The wp_reset_postdata() function looks for $this->post in the global $wp_query object which is both empty and available in admin requests. We could use the above action hook to allow us to call wp_reset_postdata(). Again, I also do not know the overarching consequences of doing this, use with caution.

发布评论

评论列表(0)

  1. 暂无评论