2017-02-27 96 views
-1

我想通過$ arg中的所有帖子運行查詢,但它僅適用於11個帖子。我已經嘗試了很多來自論壇的建議,但是我無法實現。'posts_per_page'=> -1不能正常工作

我需要做什麼?

function test_update_random_number() 
{ 
    global $post; 
    $args = array (
     'orderby'  => 'meta_value LIKE "%Yes%" rand', 
     'order'   => 'DESC', 
     'meta_key'  => 'feature_in_search', 
     'post_type'  => 'therapist', 
     'post_status' => 'publish', 
     'posts_per_page' => '-1' 
    ); 
    $myposts = new WP_Query($args); 
    if ($myposts->have_posts()) : 
     while ($myposts->have_posts()) : $myposts->the_post(); 

      $value = get_field("feature_in_search", $post_id); 
      if (strpos($value, 'es') !== false) { 
       $random_value = rand(1, 100); 
       update_field("field_58aebd8e060c0", $random_value, $post_id); 
      } else { 
       $random_value2 = rand(100, 600); 
       update_field("field_58aebd8e060c0", $random_value2, $post_id); 
      } 
     endwhile; 
     wp_reset_postdata(); 
    endif; 
} 
+0

我認爲你錯誤的參數變量類型''posts_per_page'=>'-1'//這個參數必須是int' - 參見https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters – htmlbrewery

+0

沒有文檔說-1是一個選項?我嘗試了一個像posts_per_page'=> 500的大數字。它也不起作用。 – LTech

+0

嘗試使用''posts_per_page'=> -1'也可以刪除'meta_key'和'orderby'這個參數。 – htmlbrewery

回答

2

你的問題是不是與posts_per_page參數,但與orderby參數。你有'orderby' => 'meta_value LIKE "%Yes%" rand',,這沒有多大意義。此外,如果你想隨機訂購,訂購ASCDESC也沒有意義... :)

顯然你想篩選所有具有meta_field「feature_in_search」的值爲「是」的帖子, ,並隨機排序。所以,你應該做的:

$args = [ 
    'meta_key'  => 'feature_in_search', 
    'meta_value'  => 'Yes', 
    'post_type'  => 'therapist', 
    'post_status' => 'publish', 
    'posts_per_page' => -1, 
    'orderby'  => 'rand' 
]; 

如果你真的想尋找的東西,包含「是」(和使用「LIKE」搜索),你需要建立一個適當的meta query