我想查询ACF转发器字段。我有一个称为Library的转发器字段(在名为Book的CPT中),在此转发器中,我有一个名为Library的关系字段(该字段连接到其他自定义帖子类型称为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标签的书。
这有点晚了,但是万一这对其他人有帮助的话,这对我有用:
//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();
此问题和解决方案令人困惑,因为3个字段具有相同的名称。有人可以用唯一的字段名重写Nataschas代码吗?谢谢!