简体   繁体   中英

Wordpress query update not updating all posts

I have a code to change a meta value periodically with cronjob. This cod is outside of wordpress. It updates only first 8 products.

$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'meta_key' => '_sale_price',
);

$the_query = new WP_Query($args);

while($the_query->have_posts()) : $the_query->the_post();

    update_post_meta(get_the_ID(), "_regular_price", '$new_value');

endwhile;

How can I change all the post meta values?

In your code update_post_meta(get_the_ID(), "_regular_price", '$new_value');

$new_value is inside single quotes, php wont treat it as variable. It should be in double quotes so that php translates that variable and then your update function should work as expected.

Try this:

while($the_query->have_posts()) : $the_query->the_post();

    update_post_meta(get_the_ID(), "_regular_price", "$new_value");

endwhile;

You need to Double Quote $new_value .

Is your WordPress install configured to show eight posts per page, perchance? You probably want to add 'posts_per_page'=>-1 to your argument array, so that the Loop will go through them all...

(I'm assuming your $new_value was just an example placeholder, and that you know it won't be interpolated between single quotes, as other people have mentioned...)

Please try this:

    $args = array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'posts_per_page'   => -1,
      'meta_key' => '_sale_price',
    );

posts_per_page => -1 stands for all posts.

you can try to change $args with

$args = array(
    'post_type' => 'product',      
);

PS I have not tested this;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM