admin管理员组文章数量:1317915
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 |1 Answer
Reset to default 3I 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 ofpost.php
, there is no main query - the$post
global is populated manually, usingget_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.
本文标签: wp queryTrouble with wpresetpostdata() in Admin Panel
版权声明:本文标题:wp query - Trouble with wp_reset_postdata() in Admin Panel 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742016927a2413945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_reset_postdata()
in relation to the rest of the loop? – Howdy_McGee ♦ Commented Nov 2, 2020 at 21:27