admin管理员组

文章数量:1122832

Currently trying to find only posts that match multiple custom taxonomies however im getting zero posts every time i try. How can i filter these multiple taxonomies using the and relation if some taxonomies will be left blank in the query. I need to be able to get all posts that match the theme health in life sciences at the boston campus for example. This should only return exact matches only

// Start output buffer to capture HTML

ob_start();
$tags = isset($_POST['tags']) ? $_POST['tags'] : array();
$lowercase_terms = array_map('strtolower', $tags);
$trimmed_terms = array_map('trim', $lowercase_terms);

$args = array(
    'post_type'      => 'nu_institutes',
    'posts_per_page' => -1,
    'orderby'        => 'title',
    'order'          => 'ASC',
);


if (!empty($trimmed_terms)) {
    $args['tax_query'] = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'nu_institutes-tags',
            'field'    => 'slug',
            'terms'    => $trimmed_terms,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'nu_institutes-themes',
            'field'    => 'slug',
            'terms'    => $trimmed_terms,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'nu_institutes-subjects',
            'field'    => 'slug',
            'terms'    => $trimmed_terms,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'nu_institutes-campuses',
            'field'    => 'slug',
            'terms'    => $trimmed_terms,
            'operator' => 'IN',
        ),
    );
}

$posts_query = new WP_Query($args);

本文标签: filtersHaving trouble querying multiple custom taxonomies