Interogare pentru taxonomii multiple

9 nov. 2014, 23:09:54
Vizualizări: 44.5K
Voturi: 4

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!

4
Comentarii

Adaugă un alt array în 'tax_query' => array( asta ar trebui să rezolve problema ta.

SLH SLH
9 nov. 2014 23:13:34

@SLH da, dar ce faci cu $terms = wp_get_post_terms( $post->ID, 'products-category' ); deoarece asta folosește doar termenii de taxonomie product-category?

user1374796 user1374796
9 nov. 2014 23:29:18

Atunci creează o altă variabilă $space_terms și obține termenii după ID-ul postului pentru a doua taxonomie. array( 'taxonomy' => 'space', 'field' => 'slug', 'terms' => $space_terms, ),

SLH SLH
9 nov. 2014 23:32:54

@SLH ok, dacă nu este ceva ce fac eu greșit...ai putea, te rog, să pui codul complet într-un răspuns ca să pot testa?

user1374796 user1374796
9 nov. 2014 23:51:59
Toate răspunsurile la întrebare 2
0
16

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

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

));
28 oct. 2018 06:50:31