【问题标题】:Wp_Query with multiple ACF Taxonomies - Categories and Tags具有多个 ACF 分类法的 Wp_Query - 类别和标签
【发布时间】:2021-02-02 20:28:36
【问题描述】:

我创建了一个过滤后显示,管理员可以在其中转到页面添加 ACF 块并选择哪些类别和标签,这将决定哪些帖子将显示在前端。当仅选择类别并且仅选择标签时,一切正常。但是,当同时选择类别和标签时,none show 并且默认显示所有帖子(如果没有选择任何内容,这就是我想要发生的事情。)。我的代码低于任何帮助都会很棒。如果您需要更多信息,请告诉我。谢谢。

    $categories = get_field( 'category', false, true );
if ( $categories ) {
if ( ! is_array( $categories ) ) {
$categories = array( $categories );
}
$categories = array_map( 'intval', $categories );
}

// get the ID values of the terms and not term objects.
$tags = get_field( 'tags', false, true );
if ( $tags ) {
if ( ! is_array( $tags ) ) {
$tags = array( $tags );
}
$tags = array_map( 'intval', $tags );
}

$uncat = get_cat_id( 'Uncategorized' );
$excat = get_cat_id( 'Exclude' );
$excatall = get_cat_id( 'Exclude All' );

if ( ! empty( $categories ) && ! empty( $tags ) ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'tax_query' => array( // phpcs:ignore
'relation' => 'OR',
array (
'relation' => 'AND',
array(
'taxonomy' => 'category',
'category__not_in' => array( $uncat, $excat, $excatall ),
),
array (
'taxonomy' => 'post_tag',
'terms' => $tags,
'operator' => 'IN',
),
),
array(
'taxonomy' => 'category',
'terms' => $categories,
'operator' => 'IN',
),
),
);
} elseif ( ! empty( $tags ) && empty( $categories ) ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'category__not_in' => array( $uncat, $excat, $excatall ),
'tax_query' => array( // phpcs:ignore
array(
'taxonomy' => 'post_tag',
'terms' => $tags,
),
),
);
} elseif ( ! empty( $categories ) && empty( $tags ) ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'category__not_in' => array( $uncat, $excat, $excatall ),
'tax_query' => array( // phpcs:ignore
array(
'taxonomy' => 'category',
'terms' => $categories,
),
),
);
} else {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'category__not_in' => array( $uncat, $excat, $excatall ),
);
}

【问题讨论】:

    标签: wordpress tags categories taxonomy acfpro


    【解决方案1】:

    通过在第一部分添加此内容,一切正常。

    $tax_query[] = array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => $categories,
            ),
            array(
                'taxonomy' => 'post_tag',
                'field'    => 'term_id',
                'terms'    => $tags,
            ),
        );
    
        $args = array(
            'post_type'        => 'post',
            'post_status'      => 'publish',
            'posts_per_page'   => -1,
            'order'            => ASC,
            'orderby'          => 'date',
            'category__not_in' => array( 10,11,12 ),
            'tax_query'        => $tax_query, // phpcs:ignore
        );
    

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 2019-05-22
      • 1970-01-01
      • 2017-09-18
      • 2018-09-11
      • 2018-07-14
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      相关资源
      最近更新 更多