2017-09-14 118 views
0

我试图查询在中继器字段中选中特定复选框的所有帖子。查询帖子自定义字段:ACF中继器内的检查列表

我发现这个tutorial什么,我想要做的是话题3和4的组合,但我不能由我自己完成,我的第一次尝试查询所有帖子,并打印只是一个检查项目:

<?php 

    $args = array(
     'post_type' => 'modalidade', 
     'posts_per_page' => -1, 
    ); 

    $loop = new WP_Query($args); 

?> 


<?php 

// check if the repeater field has rows of data 
if(have_rows('horarios')): 

    // loop through the rows of data 
    while (have_rows('horarios')) : the_row(); 

     $dia_da_semana = get_sub_field_object('dia_da_semana'); 
     $horario = get_sub_field_object('horario'); 

     if (in_array(1, $dia_da_semana['value'])) { 
      echo 'segunda'; 
      print_r($horario['value']); 
      echo '<br>'; 
     } 

    endwhile; 

endif; 

?> 

但是不要认为这是一个好的解决方案。也许有更好的方法来实现我想要做的事情。

+0

请更新您的问题,在[最小,完整和可验证的示例](https://stackoverflow.com/help/mcve)中包含您的相关代码,以及您迄今为止尝试的内容以及为什么它不起作用。 – FluffyKitten

+0

完成!感谢您的提醒! – carmolim

+0

你的WP Query在哪里? – Aibrean

回答

0

我使用的教程发现here,并将我的代码和我的数据结构稍微修改一下并制作完成。最终的代码如下所示:

这是对我很重要
// filter 
function my_posts_where($where) { 

$where = str_replace("meta_key = 'time_%", "meta_key LIKE 'time_%", $where); 
    return $where; 
} 

add_filter('posts_where', 'my_posts_where'); 

// args 
$args = array(
    'posts_per_page' => -1, 
    'post_type'  => 'class', 
    'meta_query' => array(
     array(
      'key'  => 'time_%_day_of_week', 
      'value'  => 'monday', 
      'compare' => 'LIKE' 
     ), 
    ) 
); 

一个变化是更换'numberposts' => -1,'posts_per_page' => -1,让所有的帖子。酒店numberposts无法使用。

相关问题