查询帖子自定义字段:ACF中继器内的检查列表

时间:2017-09-14 02:04:40

标签: mysql wordpress advanced-custom-fields

我试图查询在转发器字段中选中特定复选框的所有帖子。

我发现了这个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;

?>  

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

1 个答案:

答案 0 :(得分: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无效。

相关问题