Interogare pentru taxonomii multiple
Am o funcție configurată pentru a afișa 'Produse Similare', adică produse care împărtășesc același termen din taxonomia products-category
. Aceasta funcționează excelent, dar vreau să rafinez funcția și mai mult și să o fac mai specifică. Astfel, doresc să analizeze 2 taxonomii (product-category
și space
) pentru a găsi produse similare. Codul actual este următorul:
<?php
// Obținem termenii pentru postarea curentă
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// postarea are termeni course_type atașați
$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, //termenii taxonomiei pe care doresc să-i interoghez dinamic
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //etc...
Așadar, în prezent analizează doar taxonomia products-category
pentru produse similare (postări), dar vreau să analizeze AMBELE taxonomii product-category
și space
pentru a afișa produse similare mai specifice, dacă este posibil. Orice sugestii ar fi foarte apreciate!

În primul rând, obține toate slug-urile termenilor din taxonomia personalizată space
folosind ID-ul postului curent.
$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
$space_terms = array();
foreach( $space_terms as $term ) {
$space_terms[] = $term->slug;
}
}
Trebuie să specifici relația logică dintre fiecare matrice de taxonomie internă atunci când există mai multe.
Cheia relation
din matrice descrie relația. Valorile posibile sunt OR
și AND
.
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms,
),
array(
'taxonomy' => 'space',
'field' => 'slug',
'terms' => $space_terms,
),
),

Obțineți mai întâi toți termenii: Aici am folosit tipul de postare personalizată property ca exemplu
$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' ));
Utilizați relation
[OR] sau [AND] pentru a obține postările dorite.
$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,
),
),
));
