Query dei post correlati in un custom post type tramite custom taxonomy
Sto cercando di visualizzare i post correlati tramite una custom taxonomy e ho solo la soluzione per i post correlati che utilizzano una categoria.
$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Numero di post correlati che verranno mostrati.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<div id="relatedposts" class="clearfix"><h4>Post Correlati</h4><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<?php
if ( has_post_thumbnail() ) { ?>
<li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php echo the_post_thumbnail(); ?></a>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><div class="title"><?php the_title(); ?></div></a>
</div>
</li>
<?php }
?>
<?
}
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query();
Il codice seguente funziona solo per i tipi di post con categoria registrata e non per i tipi di tassonomie personalizzate.

okay ora ho trovato il codice che permette di utilizzare una tassonomia personalizzata per mostrare articoli correlati per un custom post type.
- $terms = get_the_terms( $post->ID , 'product_tags', 'string'); è la tassonomia personalizzata in cui dovresti inserire per interrogare tutti i tag nel tuo custom post type
- 'post_type' => 'products' è il custom post type che richiama tutti i tag personalizzati creati nella tassonomia personalizzata che in questo caso è product_tags
Inserisci questo codice ovunque all'interno del tuo loop o query per mostrare tutti gli articoli nel tuo custom post type. Questo non filtra il tuo custom post type in base a diversi tag. Questo mostra tutti i tag all'interno del tuo custom post type, che in questo caso è products.
//Ottieni l'array di termini
$terms = get_the_terms( $post->ID , 'product_tags', 'string');
//Estrai gli ID per ottenere un array di ID
$term_ids = wp_list_pluck($terms,'term_id');
//Interroga gli articoli con tax_query. Scegli 'IN' se vuoi interrogare articoli con uno qualsiasi dei termini
//Scegli 'AND' se vuoi interrogare articoli con tutti i termini
$second_query = new WP_Query( array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product_tags',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Oppure 'AND' o 'NOT IN'
)),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'post__not_in'=>array($post->ID)
) );
//Cicla attraverso gli articoli e visualizzali...
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post(); ?>
<div class="single_related">
<?php if (has_post_thumbnail()) { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail( 'related_sm', array('alt' => get_the_title()) ); ?> </a>
<?php } else { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php } ?>
</div>
<?php endwhile; wp_reset_query();
}
okay ora questo codice aiuta a risolvere il problema con l'utilizzo di custom post type per filtrare in una tassonomia personalizzata. Per esempio, avevo project come mia tassonomia e per custom post type. Quando aggiungo diversi tag a ogni project creano una tassonomia tagportfolio in cui aggiungo tutte le tassonomie e le interrogo.
