phpwordpresscategoriestaxonomyacfpro

Wp_Query with multiple ACF Taxonomies - Categories and Tags


I created a post-filter display where the admin can go to a page add a ACF block and select what category and tags and that will determine what posts will show on the front-end. Everything is working when only categories are selects and when only tags are selected. But when both categories and tags are selected none show and it defaults to showing all posts ( that is what I want to happen if nothing is selected. ). My code is below any help would be great. If you need more info please let me know. Thank you.

    $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 ),
);
}

Solution

  • Got everything to work by adding this in the first section.

    $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
        );