Michael Michael -4 years ago 73
PHP Question

Wordpress - 2 Taxonomies Filter

im a little bit stuck here.

The Build Up, i have several Pages with 2 Taxonomies. Branch and Topics.
Every Taxonomie has several Keywords.

What i now need is. Give me everything from "Topics" but only with a certain keyword from the Taxonomie Branch.

I tried it like this, but this gives me all from Topcis, without any filtering from Branch.

$termArgs = array(
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'page',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'CAT_TOPIC',
'field' => 'slug'
),
array(
'taxonomy' => 'CAT_BRANCH',
'terms' => 'solarteur',
'field' => 'slug'
),
));

$topicTerms = get_terms($termArgs);


Update:
I hade a mistake in my question. I dont need pages. I just need a list of every taxonomie from 'CAT_TOPIC', not empty, that also has a certain keyword from 'CAT_BRANCH'


I tried it like these, but i still get a list of everything from TOPIC.

$termArgs = array(
'taxonomy' => CAT_TOPIC,
'hide_empty' => true,
'parent' => 0,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'CAT_BRANCH',
'terms' => array('solarteur'),
'field' => 'slug'
)
)
);


Update 2
I now found a solution. I first get all Pages.
After that i use the page ID with get_the_terms.
Now i have a nice array, just with some duplicates in my case. Get rid of this and im done^^

Thank you @Picard

$filter = $_POST['filter'];

$termArgs = array(
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'page',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => CAT_TOPIC,
'field' => 'term_id',
'terms' => get_terms( CAT_TOPIC, array( 'hide_empty' => false, 'fields' => 'ids')),
'operator' => 'IN',
),
array(
'taxonomy' => CAT_BRANCH,
'terms' => $filter,
'field' => 'slug'
)
)
);

$topicTerms = get_posts($termArgs);
$topicslugs = array();

foreach($topicTerms as $topicTermKey => $topicTerm):
$currentTerms = get_the_terms( $topicTerm->ID, CAT_TOPIC);
$length = count($currentTerms);
for($x = 0; $x < $length ; $x++){
$topicslugs[] = array($currentTerms[$x] -> slug, $currentTerms[$x] -> name);
}
endforeach;

//remove duplicates
$topicslugs = array_unique($topicslugs, SORT_REGULAR);
$topicslugs = array_filter($topicslugs);
$topicslugs = array_values($topicslugs);

Answer Source

This should work:

$termArgs = array(
    'post_status' => 'publish',
    'orderby' => 'name',
    'order' => 'ASC',
    'post_type' => 'page',
    'tax_query' => array(
           'relation' => 'AND',
            array(
                'taxonomy' => 'CAT_TOPIC',
                'field'    => 'term_id',
                'terms'    => get_terms( 'CAT_TOPIC', array( 'hide_empty' => false, 'fields' => 'ids')),
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'CAT_BRANCH',
                'terms'    => array('solarteur'),
                'field'    => 'slug'
            )
    )
);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download