Ich habe eine Funktion eingerichtet, die "Ähnliche Produkte" anzeigt, d. H. Produkte, die denselben Taxonomiebegriff products-category
haben. Das funktioniert prima, aber ich möchte die Funktion noch weiter eingrenzen und präzisieren. Daher möchte ich, dass es sich 2 Taxonomien ansieht (product-category
und space
), um ähnliche Produkte zu finden. Das aktuelle Markup sieht wie folgt aus:
<?php
$terms = wp_get_post_terms( $post->ID, 'products-category' );
if($terms){
// post has course_type terms attached
$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, //the taxonomy terms I'd like to dynamically query
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?> //etc...
Daher wird derzeit nur die products-category
-Taxonomie für ähnliche Produkte (Posts) betrachtet, aber ich möchte, dass BEIDE product-category
- und space
-Taxonomien angezeigt werden, um nach Möglichkeit spezifischere ähnliche Produkte anzuzeigen. Anregungen wäre sehr dankbar!
Zunächst erhalten Sie alle Term-Slugs aus der benutzerdefinierten Taxonomie space
nach aktueller Beitrags-ID.
$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
$space_terms = array();
foreach( $space_terms as $term ) {
$space_terms[] = $term->slug;
}
}
Sie sollten die logische Beziehung zwischen jedem inneren Taxonomie-Array angeben, wenn es mehr als ein Array gibt.
Der Schlüssel relation
im Array beschreibt die Beziehung. Mögliche Werte sind OR
und AND
.
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'products-category',
'field' => 'slug',
'terms' => $course_terms,
),
array(
'taxonomy' => 'space',
'field' => 'slug',
'terms' => $space_terms,
),
),
Einfach zuerst alle Begriffe abrufen: Hier habe ich zum Beispiel den benutzerdefinierten Post-Typ der Eigenschaft verwendet
$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' ));
Verwenden Sie relation
[OR] oder [AND] , um die gewünschten Beiträge zu erhalten.
$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,
),
),
));