php - 如何使用选择器高级自定义字段为自定义帖子类型指定查询?
问题描述
我正在尝试过滤我的“广告系列”自定义帖子类型,并且只显示选择高级自定义字段设置为“精选广告系列”的帖子。选择 acf 的值只能为“精选广告系列”或“非精选广告系列”
到目前为止,这是我的代码,但不是显示带有“精选广告系列”选择的“广告系列”,而是显示了最近上传的“广告系列”
任何帮助,将不胜感激。提前致谢!
<?php
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'campaigns',
'meta_query' => array (
'key' => 'featured',
'value' => 'Featured Campaign'
)
);
query_posts( $args );
if (have_posts()) :
while (have_posts()) : the_post();
?>
<div class="post">
<?php the_post_thumbnail(); ?>
<h3 class="post__title heading--primary u-uppercase"><?php the_title(); ?></h3>
<p class="text-color--primary"><?php the_field(campaign_category); ?></p>
</div>
<?php
endwhile;
wp_reset_query();
endif;
?>
解决方案
感谢评论区的CBroe,答案如下:
meta_query
需要嵌套数组,即使是在一个查询的情况下。$args 数组已更改为,现在可以按预期工作:
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'campaigns',
'meta_query' => array (
array (
'key' => 'featured',
'value => 'Featured Campaign'
)
)
);
推荐阅读
- git - 从 Go 'exec()' 调用 'git shortlog' 有什么问题?
- angular - 通过Angular中的箭头键将焦点设置在下拉自动完成选项上
- android - java.lang.StackOverflowError:android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7531) 处的堆栈大小为 8MB
- oracle-apex - Oracle APEX:服务器 404 - 未加载 jQuery 或图像
- android - ARCore – 添加对不受支持的设备的支持
- java - 套接字没有响应
- c++ - 已经填充 QSet 作为可选参数
- angular - 清除 Angular 2 中的缓存
- selenium - 如何根据以下 html 定位复选框 - Selenium WebDriver 和 C#
- java - 使用 Firestore 查询是否存在潜在错误