ACF 字段查询帖子不起作用
query posts by ACF field not working
和我相关的话题很多,但我还是没有找到解决办法。我正在尝试通过 ACF 字段(单选按钮)查询帖子,但 meta_query 似乎被完全忽略了。它 returns 所有帖子,而不仅仅是符合条件的帖子。我试过使用字段键而不是字段名、其他比较等。似乎没有任何效果。希望您知道可能出了什么问题!这是我的代码:
<?php
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'product_taste',
'meta_value' => array( 'cold' ),
'compare' => 'IN',
),
array(
'meta_key' => 'product_served',
'meta_value' => array( 'grated' ),
'compare' => 'IN'
)
),
);
$query = new WP_Query( $post_args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : ?>
<?php
$query->the_post();
?>
<h5>
<?php the_title(); ?>
</h5>
<?php endwhile ?>
<?php wp_reset_postdata();
}
?>
- 在'meta_query'数组中使用'key'和'value'
您不需要在 meta_query 中使用 meta_key
和 meta_value
...您只需直接在 $args 数组中使用它们。如果要添加 meta_query 数组,则只需使用 key
和 value
,例如
$post_args = array(
[...]
'meta_query' => array(
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
[...]
2. 使用值数组查询序列化数据
当您尝试查询 ACF 数据时,将 'compare' => 'IN'
与值数组一起使用也可能会出现问题,因为 ACF 数据可以在数据库中序列化(例如,如果数据在转发器中) .
由于您只会搜索单个值,因此可以使用 LIKE
而不是 IN
。
把这些放在一起
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated',
'compare' => 'LIKE'
)
),
);
$query = new WP_Query( $post_args );
如果数据是序列化的并且您的值可以 return 多次匹配(例如 LIKE 'cold'
会匹配 "cold"、"colder"、"coldest"),然后尝试在值的末尾添加一个分号 (;
),例如
[...]
array(
'key' => 'product_taste',
'value' => 'cold;', // add ; to the end of the value
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated;', // add ; to the end of the value
'compare' => 'LIKE'
)
[...]
这将在数据库中序列化值时起作用,因为每个项目将用分号分隔。
和我相关的话题很多,但我还是没有找到解决办法。我正在尝试通过 ACF 字段(单选按钮)查询帖子,但 meta_query 似乎被完全忽略了。它 returns 所有帖子,而不仅仅是符合条件的帖子。我试过使用字段键而不是字段名、其他比较等。似乎没有任何效果。希望您知道可能出了什么问题!这是我的代码:
<?php
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'product_taste',
'meta_value' => array( 'cold' ),
'compare' => 'IN',
),
array(
'meta_key' => 'product_served',
'meta_value' => array( 'grated' ),
'compare' => 'IN'
)
),
);
$query = new WP_Query( $post_args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : ?>
<?php
$query->the_post();
?>
<h5>
<?php the_title(); ?>
</h5>
<?php endwhile ?>
<?php wp_reset_postdata();
}
?>
- 在'meta_query'数组中使用'key'和'value'
您不需要在 meta_query 中使用 meta_key
和 meta_value
...您只需直接在 $args 数组中使用它们。如果要添加 meta_query 数组,则只需使用 key
和 value
,例如
$post_args = array(
[...]
'meta_query' => array(
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
[...]
2. 使用值数组查询序列化数据
当您尝试查询 ACF 数据时,将 'compare' => 'IN'
与值数组一起使用也可能会出现问题,因为 ACF 数据可以在数据库中序列化(例如,如果数据在转发器中) .
由于您只会搜索单个值,因此可以使用 LIKE
而不是 IN
。
把这些放在一起
$post_args = array(
'post_type' => 'products',
'posts_per_page' => - 1,
'status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'product_taste',
'value' => 'cold',
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated',
'compare' => 'LIKE'
)
),
);
$query = new WP_Query( $post_args );
如果数据是序列化的并且您的值可以 return 多次匹配(例如 LIKE 'cold'
会匹配 "cold"、"colder"、"coldest"),然后尝试在值的末尾添加一个分号 (;
),例如
[...]
array(
'key' => 'product_taste',
'value' => 'cold;', // add ; to the end of the value
'compare' => 'LIKE',
),
array(
'key' => 'product_served',
'value' => 'grated;', // add ; to the end of the value
'compare' => 'LIKE'
)
[...]
这将在数据库中序列化值时起作用,因为每个项目将用分号分隔。