高级自定义字段:无法按自定义字段查询帖子
Advanced custom fields: can't query posts by custom field
我正在尝试查询 ACF 字段 "show_on_frontpage" 值等于 "yes" 的帖子(请参阅下面屏幕截图中此字段的定义)。按照 ACF docs 中的规定,这是我的代码:
$args = array(
'posts_per_page' => -1,
'meta_key' => 'show_on_frontpage',
'meta_value' => 'yes'
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
if (get_field('show_on_frontpage')) the_field('show_on_frontpage'); ?>
endwhile;
}
这个returns/displays没什么。如果我只是简单地使用 $args = array('posts_per_page' => -1);
,那么我会得到我所有的帖子,并且 "yes" 会出现在那些 "yes" 作为其 "show_on_frontpage" 字段值的帖子中。
我的代码有什么问题?
如果您使用更新的 meta_query => array()
语法,这应该有效:
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'show_on_frontpage',
'value' => 'yes',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
echo get_the_title();
// Post stuff
endwhile;
/* Restore original Post Data */
wp_reset_postdata();
}
请注意,您需要在 while 循环中将 post ID 提供给 ACF 辅助函数 get_field()
& the_field()
。
见https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
从更广泛的角度来看,这篇文章质疑为此目的使用 post_meta 键是否明智,值得一读:https://tomjn.com/2016/12/05/post-meta-abuse/。文章建议使用自定义分类法来实现您的需求 - 提高性能。
根据 ACF 论坛上的 question/answer:
https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/
最好将您的复选框字段切换为 True/False 字段,因为您的复选框组字段似乎只包含一个选项。
Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field.
If you use a true/false field then you can use WP_Query, the values of
a true/false field are 0 (zero) for false and 1 for true.
因此,如果您将复选框字段切换为 True/False 字段,您将重写代码如下:
$args = array(
'posts_per_page' => -1,
'meta_key' => 'show_on_frontpage',
'meta_value' => 1 /* or true */
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
/* My content for each post with the checkbox checked goes here */
endwhile;
}
我正在尝试查询 ACF 字段 "show_on_frontpage" 值等于 "yes" 的帖子(请参阅下面屏幕截图中此字段的定义)。按照 ACF docs 中的规定,这是我的代码:
$args = array(
'posts_per_page' => -1,
'meta_key' => 'show_on_frontpage',
'meta_value' => 'yes'
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
if (get_field('show_on_frontpage')) the_field('show_on_frontpage'); ?>
endwhile;
}
这个returns/displays没什么。如果我只是简单地使用 $args = array('posts_per_page' => -1);
,那么我会得到我所有的帖子,并且 "yes" 会出现在那些 "yes" 作为其 "show_on_frontpage" 字段值的帖子中。
我的代码有什么问题?
如果您使用更新的 meta_query => array()
语法,这应该有效:
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'show_on_frontpage',
'value' => 'yes',
'compare' => 'LIKE',
)
),
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
echo get_the_title();
// Post stuff
endwhile;
/* Restore original Post Data */
wp_reset_postdata();
}
请注意,您需要在 while 循环中将 post ID 提供给 ACF 辅助函数 get_field()
& the_field()
。
见https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
从更广泛的角度来看,这篇文章质疑为此目的使用 post_meta 键是否明智,值得一读:https://tomjn.com/2016/12/05/post-meta-abuse/。文章建议使用自定义分类法来实现您的需求 - 提高性能。
根据 ACF 论坛上的 question/answer:
https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/
最好将您的复选框字段切换为 True/False 字段,因为您的复选框组字段似乎只包含一个选项。
Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field.
If you use a true/false field then you can use WP_Query, the values of a true/false field are 0 (zero) for false and 1 for true.
因此,如果您将复选框字段切换为 True/False 字段,您将重写代码如下:
$args = array(
'posts_per_page' => -1,
'meta_key' => 'show_on_frontpage',
'meta_value' => 1 /* or true */
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
while ($my_posts->have_posts()) : $my_posts->the_post();
/* My content for each post with the checkbox checked goes here */
endwhile;
}