【问题标题】:Query on a ACF repeater field查询 ACF 转发器字段
【发布时间】:2018-10-06 22:22:27
【问题描述】:

我想查询一个 ACF 转发器字段。我有一个名为 Library 的转发器字段(在名为 Book 的 CPT 中),在这个转发器中,我有一个名为 Library 的关系字段(连接到另一个名为 Library 的自定义帖子类型)。我想查询的正是这个字段*。

由于用户提供的唯一值(通过选择而被选中),我希望恢复与此图书馆相关的所有书籍。

Ex:选择了“库 1”。返回:“哈利波特 1”和“哈利波特 2”。

试用(不工作)

function my_posts_where( $where ) {

$where = str_replace("meta_key = 'library_$", "meta_key LIKE 'library_%", $where);
return $where; } add_filter('posts_where', 'my_posts_where');
$library= $_GET['library'];

$v_args = array(
        'post_type'     =>  'book', 
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'library_$_library',
                                    'compare' => '=', 
                                    'value'   => $library, 
                                ),
                            )
    ); $Query = new WP_Query($v_args);

    if($Query->have_posts()) :
        while($Query->have_posts()) : $Query->the_post();
            ...
        endwhile;

    else : 
        ...
    endif;

还有这个

    $library= $_GET['library'];

    $v_args = array(
        'post_type'     =>  'book', 
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'library',
                                    'value'   => $library,
                                    'compare' => 'LIKE', 
                                ),
                            )
    );

$Query = new WP_Query($v_args); 

我在互联网上搜索,我无法解决我的问题......

非常感谢。

-* :我还有一个名为 Tags 的转发器字段,其中有一个与标签相关的分类字段。我想展示所有带有 thig 标签的书。

【问题讨论】:

    标签: php wordpress custom-post-type advanced-custom-fields


    【解决方案1】:

    这有点晚了,但如果这对其他人有帮助,这对我有用:

    //Since the changed behaviour of esc_sql() in WordPress 4.8.3, 
    //cannot use the % character as a placeholder, hence need to alter 'where' close:
    function my_posts_where( $where ) {
        $where = str_replace("meta_key = 'library_$", "meta_key LIKE 'library_%", $where);
        return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    $library_id = $_GET['library']; //get querystr var
    
    $args = array(
         'post_type'      => 'book',
         'posts_per_page' => -1,
         'meta_query'     => array(
              array(
                   'key' => 'library_$_library', // our repeater field post object
                   'value' => '"'. $library_id .'"', //matches exactly "123", not just 123 - prevents a match for "1234"
                   'compare' => 'LIKE'
              )
         )
    );
    $query = new WP_Query($args);
    
    if ($query->have_posts()): 
        while ($query->have_posts()) : $query->the_post();
            echo $post->post_title.'<br>';
        endwhile; 
    endif;
    
    wp_reset_query();
    

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2021-04-26
      • 2019-05-24
      • 2014-07-07
      • 1970-01-01
      相关资源
      最近更新 更多