【问题标题】:Array not working as intended in get_terms function数组在 get_terms 函数中未按预期工作
【发布时间】:2018-08-03 03:28:40
【问题描述】:

我正在调用 WordPress get_terms 函数,但我想排除多个类别。

为什么这无效?

$terms = get_terms( 'category', 'exclude' = array(1, 238) );

我发现我必须使用这个:

$terms = get_terms( 'category', array('exclude' => array(1, 238),) );

但这似乎是多余的,因为高阶数组中只有一项。

参考文档:https://developer.wordpress.org/reference/functions/get_terms/

【问题讨论】:

    标签: php arrays wordpress syntax-error


    【解决方案1】:

    你可以使用:

    $taxonomy  = 'review-product';
    
    $post_type = 'reviews';
    
    $args = [
    
        'post_type' => $post_type,
    
        'tax_query' => [
            [
                'taxonomy' => $taxonomy,
                'terms'    => get_terms( $taxonomy, [ 'fields' => 'ids'  ] ),
                'operator' => 'NOT IN'
            ]
        ]
    ];
    
    $query = new \WP_Query( $args );
    

    【讨论】:

    • 您能否解释或注释代码行,因为我是 PHP 新手。谢谢。
    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 2018-02-19
    • 2019-10-30
    • 2015-04-11
    • 2022-01-25
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多