Query su tassonomie multiple
Ho una funzione configurata per mostrare 'Prodotti Simili', cioè prodotti che condividono lo stesso termine della tassonomia products-category
. Funziona benissimo ma voglio restringere ulteriormente la funzione e renderla più specifica. Quindi voglio che esamini 2 tassonomie (product-category
e space
) per trovare prodotti simili. Il codice attuale è il seguente:
<?php
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// il post ha dei termini course_type associati
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'regularproducts',
'tax_query' => array(
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms, //i termini della tassonomia che vorrei interrogare dinamicamente
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //ecc...
Attualmente esamina solo la tassonomia products-category
per i prodotti simili (post), ma voglio che esamini SIA product-category
CHE space
per visualizzare prodotti simili più specifici, se possibile. Qualsiasi suggerimento sarebbe molto apprezzato!

Innanzitutto, ottieni tutti gli slug dei termini dalla tassonomia personalizzata space
in base all'ID del post corrente.
$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
$space_terms = array();
foreach( $space_terms as $term ) {
$space_terms[] = $term->slug;
}
}
Dovresti specificare la relazione logica tra ogni array di tassonomia interna quando ce n'è più di uno.
La chiave relation
nell'array descrive la relazione. I valori possibili sono OR
e AND
.
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms,
),
array(
'taxonomy' => 'space',
'field' => 'slug',
'terms' => $space_terms,
),
),

Prima ottieni tutti i termini: Qui ho usato come esempio il custom post type "property"
$property_statu_terms = wp_get_post_terms( $post->ID, 'property_status', array( 'fields' => 'ids' ));
$property_type_terms = wp_get_post_terms( $post->ID, 'property_type', array( 'fields' => 'ids' ));
$property_city_terms = wp_get_post_terms( $post->ID, 'property_city', array( 'fields' => 'ids' ));
$property_state_terms = wp_get_post_terms( $post->ID, 'property_state', array( 'fields' => 'ids' ));
$property_feature_terms = wp_get_post_terms( $post->ID, 'property_feature', array( 'fields' => 'ids' ));
Usa relation
[OR] o [AND] per ottenere i post desiderati.
$query = new WP_Query( array(
'post_type' => 'property',
'posts_per_page' => 3,
'orderby' => 'rand',
'post__not_in' => array( $post->ID ),
'ignore_sticky_posts' => 1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'property_status',
'field' => 'id',
'terms' => $property_statu_terms,
),
array(
'taxonomy' => 'property_type',
'field' => 'id',
'terms' => $property_type_terms,
),
array(
'taxonomy' => 'property_city',
'field' => 'id',
'terms' => $property_city_terms,
),
array(
'taxonomy' => 'property_state',
'field' => 'id',
'terms' => $property_state_terms,
),
array(
'taxonomy' => 'property_feature',
'field' => 'id',
'terms' => $property_feature_terms,
),
),
));
