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 badges
Add a comment  | 

2 Answers 2

Reset to default 2

Having 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