Ich verwende derzeit den folgenden Code, um eine Liste mit Links zu Beiträgen in einem bestimmten CPT und einer bestimmten Taxonomie anzuzeigen:
<?php
$custom_terms = get_terms('videoscategory');
foreach(array($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'videoscategory',
'field' => 'slug',
'terms' => $custom_term->slug
),
array(
'taxonomy' => 'product_category',
'field' => 'slug',
'terms' => $other_custom_term->slug
),
)
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';
while($loop->have_posts()) : $loop->the_post();
echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
endwhile;
}
} ?>
Es funktioniert einwandfrei, aber ich möchte nur Posts anzeigen, die in beiden Taxonomien enthalten sind. Was muss ich hinzufügen, um dies zu tun?
Jede Hilfe ist dankbar, danke.
Ok, ich habe es herausgefunden!
<?php
$custom_terms = get_terms('your_other_category');
$other_custom_terms = get_terms('your_category');
foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
wp_reset_query();
$args = array('post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'your_category',
'field' => 'slug',
'terms' => $other_custom_term->slug
),
array(
'taxonomy' => 'your_other_category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';
while($loop->have_posts()) : $loop->the_post();
echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
endwhile;
}
}
} ?>
Laut dem Codex würden Sie Posts aus mehreren Taxonomien folgendermaßen abfragen:
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'videoscategory',
'field' => 'slug',
'terms' => $custom_term->slug
),
array(
'taxonomy' => 'yourothertaxonomy',
'field' => 'slug',
'terms' => $other_custom_term->slug
)
)