Wordpress 查询更新不更新所有帖子

Wordpress query update not updating all posts

我有一个代码可以使用 cronjob 定期更改元值。这个鳕鱼在 wordpress 之外。它仅更新前 8 个产品。

$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;

如何更改所有 post 元值?

您可以尝试将 $args 更改为

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

P.S我没有测试过这个;

在您的代码中 update_post_meta(get_the_ID(), "_regular_price", '$new_value');

$new_value 在单引号内,php 不会将其视为变量。它应该用双引号引起来,以便 php 转换该变量,然后您的更新函数应该按预期工作。

试试这个:

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.

您的 WordPress 安装是否配置为每页显示 8 个帖子?您可能想将 'posts_per_page'=>-1 添加到您的参数数组,以便循环将遍历它们...

(我假设您的 $new_value 只是一个示例占位符,并且您知道它不会像其他人提到的那样在单引号之间插入...)

请试试这个:

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

posts_per_page => -1 代表所有帖子。