Mostrare post correlati in modo casuale che corrispondono a "qualsiasi" tag esistente?

7 apr 2015, 21:16:58
Visualizzazioni: 14.2K
Voti: 2

Vorrei sapere quale codice dovrei aggiungere per far sì che i miei post correlati per tag vengano visualizzati in modo casuale? Ho preso questo codice da un'altra parte.

<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Numero di post correlati da visualizzare.
'caller_get_posts'=>1,
'orderby' => 'rand' // Aggiunto per ordinare i risultati casualmente
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related"><h4>Post Correlati</h4>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="ncc">
<h5><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>" rel="nofollow"><?php the_title(); ?></a></h5>
<?php the_excerpt(); ?>

</div><!--ncc-->

<? }
echo '</div><!--related-->';
}
}
$post = $orig_post;
wp_reset_query(); ?>

Qualche soluzione? Grazie!

3
Commenti

Ho solo bisogno di sapere come escludere un tag

Moreentje Moreentje
31 mar 2022 12:32:07
Tutte le risposte alla domanda 1
5
17

Parte del codice nell'OP è un po' datato e deprecato, come caller_get_posts che è stato deprecato anni fa. Il parametro corretto da utilizzare ora è ignore_sticky_posts. La tua query è anche molto inefficiente e non buona per le prestazioni.

Ecco come affronterei questo problema

  • Utilizza get_queried_object_id() per ottenere l'ID del post corrente invece del metodo meno affidabile che usa il globale $post

  • Usa wp_get_post_terms() per restituire tutti gli ID dei tag assegnati al post. Da quello che capisco, abbiamo solo bisogno di ottenere gli ID, non gli oggetti tag completi

  • Usa un corretto tax_query per ottenere tutti i post che hanno uno di questi tag associati. Questa è più una preferenza personale in quanto è facile cambiare tra taxonomy e anche, guardando il codice sorgente, i parametri dei tag usano un tax_query

  • Usa rand come valore per il parametro orderby in WP_Query

In breve, mettendo tutto in codice: (Non testato, richiede PHP 5.4+)

<?php
$tags = wp_get_post_terms( get_queried_object_id(), 'post_tag', ['fields' => 'ids'] );
$args = [
    'post__not_in'        => array( get_queried_object_id() ),
    'posts_per_page'      => 5,
    'ignore_sticky_posts' => 1,
    'orderby'             => 'rand',
    'tax_query' => [
        [
            'taxonomy' => 'post_tag',
            'terms'    => $tags
        ]
    ]
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
    echo '<div id="related"><h4>Post Correlati</h4>';
        while( $my_query->have_posts() ) {
            $my_query->the_post(); ?>
            <div class="ncc">

                <h5><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>
                <?php the_excerpt(); ?>

            </div><!--ncc-->
        <?php }
        wp_reset_postdata();
    echo '</div><!--related-->';
}
?> 
7 apr 2015 21:47:36
Commenti

Ciao Peter Goosen, fantastico! Il tuo codice funziona alla perfezione! Grazie mille! :)

Jornes Jornes
8 apr 2015 06:12:38

È stato un piacere, divertiti :-)

Pieter Goosen Pieter Goosen
8 apr 2015 08:49:21

wow sembra fantastico. Un'altra domanda: come possiamo escludere i post più popolari da questa query? Le visualizzazioni dei post sono memorizzate in un campo personalizzato chiamato (pageView)? È possibile?

Fatih Toprak Fatih Toprak
12 ott 2016 05:03:35

Funziona ancora bene in PHP 8... è la qualità Goosen ;)

Jesse Nickles Jesse Nickles
27 gen 2024 11:39:46

Al momento non posso modificare, ma lascio una nota qui che lo snippet rel="nofollow" dovrebbe essere eliminato da questa risposta.

Jesse Nickles Jesse Nickles
29 gen 2024 09:27:17