繁体   English   中英

在ACF转发器字段上查询

[英]Query on a ACF repeater field

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

由于用户提供的唯一值(由于选择而被选中),我希望恢复与该Library有关的所有书籍。

例如 :选择“库1”。 返回:'Harry Potter 1'和'Harry Potter 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); 

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

非常感谢。

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

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

//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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM