ACF 的 WordPress 查询帖子
WordPress query posts by ACF
更新 - 已解决
好的,所以我有三个自定义 post 类型 'courses'、'modules' 和 'results'。
这些 post 类型设置了一些 ACF(高级自定义字段)。
我正在尝试根据他们使用 'meta_query' 的 ACF 查询 'results' CPT。
我尝试查询的两个 ACF 字段是 'module' 和 'user'。
- 'module' - 设置为'Relationship'字段类型过滤
'module' 自定义 post 类型。
- 'user' - 设置为(关系)'User' 字段类型。
我好像只得到一个空数组。我看过 https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#custom-field%20parameters 上的示例,仍然没有任何运气。
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => $module_id,
'compare' => '=',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
调用函数:
print_r(check_results_for_user($post->ID, $currentUserID ));
结果:
Array ( )
更新 - 已解决:
好的,所以设法弄清楚了。是由于 'module' 关系字段将其数据保存为序列化数组:a:1:{i:0;s:1:"9";}
所以你必须将比较更改为 'LIKE' 并将值包装在 "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
见本页结尾:https://www.advancedcustomfields.com/resources/querying-relationship-fields/
更新 - 已解决:
好的,所以设法解决了。是由于 'module' 关系字段将其数据保存为序列化数组:a:1:{i:0;s:1:"9";}
所以你必须将比较更改为 'LIKE' 并将值包装在 "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
见本页结尾:https://www.advancedcustomfields.com/resources/querying-relationship-fields/
更新 - 已解决
好的,所以我有三个自定义 post 类型 'courses'、'modules' 和 'results'。 这些 post 类型设置了一些 ACF(高级自定义字段)。
我正在尝试根据他们使用 'meta_query' 的 ACF 查询 'results' CPT。
我尝试查询的两个 ACF 字段是 'module' 和 'user'。
- 'module' - 设置为'Relationship'字段类型过滤 'module' 自定义 post 类型。
- 'user' - 设置为(关系)'User' 字段类型。
我好像只得到一个空数组。我看过 https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#custom-field%20parameters 上的示例,仍然没有任何运气。
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => $module_id,
'compare' => '=',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
调用函数:
print_r(check_results_for_user($post->ID, $currentUserID ));
结果:
Array ( )
更新 - 已解决:
好的,所以设法弄清楚了。是由于 'module' 关系字段将其数据保存为序列化数组:a:1:{i:0;s:1:"9";}
所以你必须将比较更改为 'LIKE' 并将值包装在 "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
见本页结尾:https://www.advancedcustomfields.com/resources/querying-relationship-fields/
更新 - 已解决:
好的,所以设法解决了。是由于 'module' 关系字段将其数据保存为序列化数组:a:1:{i:0;s:1:"9";}
所以你必须将比较更改为 'LIKE' 并将值包装在 "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
见本页结尾:https://www.advancedcustomfields.com/resources/querying-relationship-fields/