[英]Query on a ACF repeater field
I would like to query an ACF repeater field. 我想查询ACF转发器字段。 I've a repeater field called Library (in a CPT called Book ) and, in this repeater, I have a relation field called Library (that is connected to an other custom post type called Library ). 我有一个称为Library的转发器字段(在CPT中称为Book ),在这个转发器中,我有一个称为Library的关系字段(该字段与另一个称为Library的自定义帖子类型相连)。 It is this field that I would like to query*. 我想查询的是此字段*。
I look to recover, thanks to an unique value given by the user (selected thanks to a select), all the books that are in relation with this Library . 由于用户提供的唯一值(由于选择而被选中),我希望恢复与该Library有关的所有书籍。
Ex : 'Library 1' is selected. 例如 :选择“库1”。 Return : 'Harry Potter 1' and 'Harry Potter 2'. 返回:'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;
And this 和这个
$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);
I searched on the Internet, I could not resolve my problem ... 我在互联网上搜索,无法解决我的问题...
Thanks a lot. 非常感谢。
-* : I also have a repeater field called Tags and in it a Taxonomy field in relation with tags. -*:我也有一个称为标签的转发器字段,其中有一个与标签相关的分类字段。 I would like to show all the books that have thig tag. 我想展示所有带有thig标签的书。
This is a bit late, but in case this helps anyone else, this works for me: 这有点晚了,但是如果这对其他人有帮助的话,这对我有用:
//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.