仅当特定自定义字段在这些页面中不为空时才列出子页面
List child pages only if specific custom field is not empty in those pages
所以我有这个功能来列出所有子页面,我希望它不列出那些没有自定义字段 "role" 或者如果这个自定义字段为空的元素。我尝试了不同的方法,但即使我输入值或元值 "test",也会列出所有页面。此查询有问题 meta_key 工作正常,但 meta_value 不工作。
我的函数代码是这样的:
function list_child_pages() {
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'role',
'meta_value' => 'test',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
return list_childs();
} else
$string="team members were not found";
return $string;
}
'meta_query' => [
'relation' => 'AND',
[
'key' => 'role',
'value' => 'test',
'compare' => '='
]
],
我更喜欢使用 []
而不是 array()
不过你也可以
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
与您的示例集成:
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
);
尝试阅读 Class WP_Meta_Query
的文档:https://codex.wordpress.org/Class_Reference/WP_Meta_Query#Initializing_WP_Meta_Query
所以我有这个功能来列出所有子页面,我希望它不列出那些没有自定义字段 "role" 或者如果这个自定义字段为空的元素。我尝试了不同的方法,但即使我输入值或元值 "test",也会列出所有页面。此查询有问题 meta_key 工作正常,但 meta_value 不工作。
我的函数代码是这样的:
function list_child_pages() {
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'role',
'meta_value' => 'test',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
return list_childs();
} else
$string="team members were not found";
return $string;
}
'meta_query' => [
'relation' => 'AND',
[
'key' => 'role',
'value' => 'test',
'compare' => '='
]
],
我更喜欢使用 []
而不是 array()
不过你也可以
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
与您的示例集成:
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'role',
'value' => 'test',
'compare' => '='
)
),
);
尝试阅读 Class WP_Meta_Query
的文档:https://codex.wordpress.org/Class_Reference/WP_Meta_Query#Initializing_WP_Meta_Query