【问题标题】:Query posts custom field: check list inside a ACF repeater查询帖子自定义字段:ACF 中继器内的检查列表
【发布时间】:2018-02-22 20:44:01
【问题描述】:

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

我找到了这个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;

?>  

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

【问题讨论】:

  • 请更新您的问题,将您的相关代码包含在Minimal, Complete, and Verifiable example 中,并详细说明您迄今为止尝试过的内容以及为什么它不起作用。
  • 完成!感谢提醒!
  • 你的 WP 查询在哪里?
  • 我只是在查询一个CTP的所有帖子,我现在在问题中添加了。

标签: mysql wordpress advanced-custom-fields


【解决方案1】:

我使用找到的教程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' =&gt; -1, 替换为'posts_per_page' =&gt; -1, 以获取所有帖子。 numberposts 属性不起作用。

【讨论】:

    猜你喜欢
    • 2019-06-27
    • 2020-12-13
    • 2021-01-03
    • 2021-06-29
    • 2015-10-24
    • 2020-05-22
    • 1970-01-01
    • 2017-02-22
    • 2014-03-17
    相关资源
    最近更新 更多