admin管理员组文章数量:1124691
Have 2 plugins which power some extra woocommerce custom options in WP. Both hook to plugins_loaded.
The 1st plugin is not resetting to the original post data by the looks of things.
One plugin is a sample drop down with the post metaboxes. The plugin queries the db using WP_Query to output a select options box. I run wp_reset_postdata() after.
The second plugin is a different select box which is not pulling with the correct post id so the meta value never correctly saves as the ID is incorrect and also never selects the correct option on load.
Disabling the first sample plugin the second plugin works just like the 1st does because no other queries are running before.
The post data is being requested to be reset but nothing happens.
public function get_samples_array(){
$args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $this->sample_product_category_id
)
)
);
$results = new WP_Query( $args );
if ( $results->have_posts() ) {
$array[]='None';
while ( $results->have_posts() ) : $results->the_post();
$array[get_the_ID()]=get_the_title();
endwhile;
}
wp_reset_postdata();
return $array;
}
Above is the section of code I am using.
It just appears that wp_reset_postdata() is not working. Ive tried a variety of things with no success that I am stumped.
I don't want to have to go using something bad practice like intval($_GET['post']) to make it work.
Could it be where I am hooking the plugins that its making wp_reset_postdata() not function as expected?
Have 2 plugins which power some extra woocommerce custom options in WP. Both hook to plugins_loaded.
The 1st plugin is not resetting to the original post data by the looks of things.
One plugin is a sample drop down with the post metaboxes. The plugin queries the db using WP_Query to output a select options box. I run wp_reset_postdata() after.
The second plugin is a different select box which is not pulling with the correct post id so the meta value never correctly saves as the ID is incorrect and also never selects the correct option on load.
Disabling the first sample plugin the second plugin works just like the 1st does because no other queries are running before.
The post data is being requested to be reset but nothing happens.
public function get_samples_array(){
$args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $this->sample_product_category_id
)
)
);
$results = new WP_Query( $args );
if ( $results->have_posts() ) {
$array[]='None';
while ( $results->have_posts() ) : $results->the_post();
$array[get_the_ID()]=get_the_title();
endwhile;
}
wp_reset_postdata();
return $array;
}
Above is the section of code I am using.
It just appears that wp_reset_postdata() is not working. Ive tried a variety of things with no success that I am stumped.
I don't want to have to go using something bad practice like intval($_GET['post']) to make it work.
Could it be where I am hooking the plugins that its making wp_reset_postdata() not function as expected?
Share Improve this question asked Jul 6, 2018 at 23:53 JimboJimbo 1512 silver badges13 bronze badges2 Answers
Reset to default 2Having attempted various work arounds the way I have this to work is to not use the_post(). WP_Query should still work but I have swapped over to get_posts().
$results = get_posts( $args );
foreach ( $results as $p ):
$array[$p->ID] = $p->post_title;
endforeach;
return $array;
Frustrating that this seems to be a problem with certain admin pages.
Here is a ticket related to the issue: https://core.trac.wordpress.org/ticket/18408
Old post but the WP ticket still exists, so a workaround is to define $post to a temp variable and reassign it after the query.
global $post
$tmp_post = $post;
$args = array(...);
$results = new WP_Query( $args );
if ( $results->have_posts() ) {
...
}
$post = $tmp_post;
本文标签: wp queryAdmin Page Post Id wpresetpostdata not working
版权声明:本文标题:wp query - Admin Page Post Id wp_reset_postdata not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736643053a1946041.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论