2015-10-14 109 views
0

我有一个代码,用cronjob定期更改元值。这个鳕鱼不属于wordpress。它只更新前8个产品。WordPress的查询更新没有更新所有文章

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

如何更改所有后期元值?

回答

0

就是你的WordPress安装配置每页显示八个员额,或许?您可能想要将'posts_per_page'=>-1添加到您的参数数组中,以便循环将遍历它们全部...

(我假设您的$new_value只是一个示例占位符,并且您知道它不会被插值单引号,因为其他人都提到...)之间

+0

谢谢您的回复。它工作得很好。正如你所说的那样,这仅仅是一个例子。但是当我挖掘该项目时,我发现我需要更多的查询。我可以通过“$ data * _regular_price”更改此值,其中仅_sale_price = 10? –

-1

你可以尝试改变的$ args与

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

P.S我没有测试过这一点;

1

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

$new_value是单引号,PHP不会把它当作变量。它应该在双引号,以便PHP翻译该变量,然后您的更新功能应按预期工作。

0

试试这个:

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

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

endwhile; 

您需要双引号$new_value

0

请试试这个:

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

posts_per_page => -1表示所有的帖子。