查询帖子自定义字段:ACF 转发器内的检查列表
Query posts custom field: check list inside a ACF repeater
我正在尝试查询在转发器字段中选中了特定复选框的所有帖子。
我找到了这个 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;
?>
但不要认为这是一个好的解决方案。也许有更好的方法来实现我想要做的事情。
我使用了找到的教程 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
不工作。
我正在尝试查询在转发器字段中选中了特定复选框的所有帖子。
我找到了这个 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;
?>
但不要认为这是一个好的解决方案。也许有更好的方法来实现我想要做的事情。
我使用了找到的教程 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
不工作。