2015-10-14 39 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表示所有的帖子。