【问题标题】:List child pages only if specific custom field is not empty in those pages仅当特定自定义字段在这些页面中不为空时才列出子页面
【发布时间】:2017-03-14 22:24:00
【问题描述】:

所以我有这个功能来列出所有子页面,我希望它不列出那些没有自定义字段“角色”或者如果这个自定义字段为空的元素。我尝试了不同的方法,但即使我输入值或元值“测试”,所有页面都会列出。这个查询有问题,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;
}

【问题讨论】:

  • 在你的循环中怎么样,你可以 if(get_field('role')){ //echo your stuff here }

标签: wordpress advanced-custom-fields


【解决方案1】:
'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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 2011-03-25
    • 2014-02-19
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多