Come mostrare post correlati per categoria o custom post type?
Sto cercando il modo migliore per creare, probabilmente, un custom post type dove poter filtrare nelle categorie correlate a quel custom post type nella sidebar.
Ecco il sito su cui sto lavorando on. Originariamente i profili degli studenti e dei donatori dovrebbero essere un tipo di post o categoria che può essere aggiornato dinamicamente quando viene creato un nuovo profilo. Quindi Health & Science, Student Success e Creative community hanno i loro set specifici di profili.
Quindi diciamo che John Doe è un nuovo profilo studente per Student Success, posso assegnargli un tag per farlo apparire solo in quella pagina e quando visualizzo il suo singolo post ho problemi a mostrare le categorie o i profili correlati, in questo caso Student Success. Quindi quando si visualizza il profilo di John Doe, gli altri profili sotto Student Success dovrebbero essere elencati nella sidebar.
Ho notato che WordPress condivide il template file single.php, quindi la sidebar che ho per il file single.php viene utilizzata anche per i profili, cosa che non vogliamo.
In sintesi, se scorri Health & Science, Student Success e Creative community, sto cercando un setup simile ma invece di un processo in 4 passi che il client deve seguire quando carica un nuovo profilo, cerco un processo in un solo passaggio.
Il mio attuale setup prevede che si aggiunga il nuovo profilo come pagina, poi si vada nell'area menu nel dashboard e si aggiunga il nuovo profilo al menu.

<?php
// Ottieni post correlati dalla stessa categoria, escludendo il post corrente
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5,'post_type' => 'news', 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<li>
<?php the_post_thumbnail(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php }
wp_reset_postdata(); ?>

Per prima cosa devi creare una query con una tassonomia personalizzata o qualsiasi cosa tu voglia utilizzare per mostrare i tuoi articoli
function related_posts_by_taxonomy( $post_id, $taxonomy, $args=array() ) {
$query = new WP_Query();
$terms = wp_get_object_terms( $post_id, $taxonomy );
// Assicurati di avere i termini dal post corrente
if ( count( $terms ) ) {
$post_ids = get_objects_in_term( $terms[0]->term_id, $taxonomy );
$post = get_post( $post_id );
$post_type = get_post_type( $post );
$args = wp_parse_args( $args, array(
'post_type' => $type,
'post__in' => $post_ids,
'taxonomy' => $taxonomy,
'term' => $terms[0]->slug,
) );
$query = new WP_Query( $args );
}
// Restituisci i risultati in formato query
return $query;
}
Per elencare l'output puoi utilizzare il codice seguente
$related = related_posts_by_taxonomy( $post->ID, 'service_category' );
while ( $related->have_posts() ): $related->the_post(); ?>
<li class="folk-grid__col all">
<img class="" data-src="<?php echo get_template_directory_uri(); ?>/images/icons-service/<?php $icon_service = get_post_meta( $post->ID, 'service_options_icons', true ); echo $icon_service; ?>.svg" alt="<?php echo get_the_title( $post->ID ); ?>" src="<?php echo get_template_directory_uri(); ?>/images/icons-service/<?php echo $icon_service; ?>.svg">
<span><a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><?php echo get_the_title( $post->ID ); ?></a></span>
<div>
<a rel="nofollow" class="btn folk-button" style="top: 35%;width: 62%;padding: 10px;" href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>">Richiedi ora</a>
</div>
</li>
endwhile;
