php - WordPress 查询以获取 ACF 字段的正确计数?
问题描述
当前帖子的首次提交字段(acf 字段)为空。
所以,查询应该显示 0,但它显示 1。
我尝试了以下代码,它们都显示 1。
请告诉我如何获得正确的计数 0?
$posts = array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_title' => get_the_title(),
'post_type' => 'infosubmission',
'meta_query' => array(
array(
'key' => 'firstsubmission',
'value' => 'done'
)
)
);
$post_b = new WP_Query( $posts );
$the_count = count($post_b);
echo 'COUNT:' .$the_count;
$posts = get_posts(array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_title' => get_the_title(),
'post_type' => 'infosubmission',
'meta_key' => 'firstsubmission',
'meta_value' => 'done'
));
$the_query = new WP_Query( $posts );
$the_count = count($the_query);
echo 'COUNT:' .$the_count;
$infopost = [
'author' => get_current_user_id(),
'post_type' => 'infosubmission',
'posts_per_page' => -1,
'post_title' => get_the_title(),
'firstsubmission' => 'done'
];
$info_posts = new WP_Query($infopost);
$info_count = count($info_posts);
echo 'COUNT:' .$info_count;
解决方案
/* please replace below snippet in your code */
$posts = get_posts(array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_title' => get_the_title(),
'post_type' => 'infosubmission',
'meta_key' => 'firstsubmission',
'meta_value' => 'done',
'meta_compare' => '='
));
推荐阅读
- c# - XML 键值对 C#
- c - 标准 C 是否接受 `{0}` 作为任何结构的初始值设定项?
- influxdb - 我可以有 2 个独立的 InfluxDB 安装吗?
- javascript - 将对象转换为带有对象的数组
- docker - 如何在 Docker Compose 中从服务器设置环境变量?
- zapier - 将字符串添加到 Zapier 中的空列表
- objective-c - Objective-c等价于java匿名接口实现
- java - 继承 java-bank 账户 (java)
- json - 将 JSON 字段转换为 ReasonML 变体
- c++ - 从类成员函数构造 std::thread