Query su tassonomie multiple

9 nov 2014, 23:09:54
Visualizzazioni: 44.5K
Voti: 4

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!

4
Commenti

Aggiungi un altro array in 'tax_query' => array( questo dovrebbe risolvere il tuo problema.

SLH SLH
9 nov 2014 23:13:34

@SLH sì, ma riguardo a $terms = wp_get_post_terms( $post->ID, 'products-category' ); visto che questo utilizza solo il termine della tassonomia product-category?

user1374796 user1374796
9 nov 2014 23:29:18

Allora crea un'altra variabile $space_terms e ottieni i termini tramite ID del post per la tua seconda tassonomia. array( 'taxonomy' => 'space', 'field' => 'slug', 'terms' => $space_terms, ),

SLH SLH
9 nov 2014 23:32:54

@SLH ok, a meno che non sia qualcosa che sto sbagliando io... saresti per caso in grado di inserire il codice completo in una risposta così posso testarlo?

user1374796 user1374796
9 nov 2014 23:51:59
Tutte le risposte alla domanda 2
0
16

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,
    ),
),
10 nov 2014 00:02:43
0

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,
        ),
    ),

));
28 ott 2018 06:50:31