Related Posts Wordpress

Asked

Viewed 40 times

1

Fala galera,

I am developing a wordpress theme, and on a page I use a function to pick up related posts, it picks up all posts to the current. But he wouldn’t need to take the current one, what would be the way to delete the current post and actually show only related ones?

NOTE: I am using a custom_post_type=courses

Thanks!!!

function postagem_relacionada() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && is_wp_error($categories)):
        foreach ($categories as $category):
            array_push($cat_ids, $category->term_id);
        endforeach;
    endif;

    $current_post_type = get_post_type($post_id);
    $query_args = array( 

        'category__in'   => $cat_ids,
        'post_type'      => $current_post_type,
        'post_not_in'    => array($post_id),
        'posts_per_page'  => '10'

     );

    $related_cats_post = new WP_Query( $query_args );

    if($related_cats_post->have_posts()):
         while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
           <div class="card text-gray height-100p shadow-v2">
              <a href="<?php the_permalink(); ?>">
                <img class="card-img-top" src="<?php echo get_field('imagem_do_curso') ?>" alt="">
              </a>
              <div class="p-4">
                <a href="<?php the_permalink(); ?>" class="h6">
                  <?php the_title(); ?>
                </a>
                <button class="btn btn-primary btn-sm btn-pill" style="color: #191B31!important">
                  + <?php echo get_field('quantidade_de_alunos_formados') ?> Alunos formados
                </button>
              </div>
            </div>   
        <?php endwhile;

        // Restore original Post Data
        wp_reset_postdata();
     endif;

}

2 answers

2


Talk partner, all beauty?

You’ll get it this way below.

In the single-courses.php of your theme, you should add:

    <?php 
        $currentID = get_the_ID(); 
        query_posts( 
            array(
                'posts_per_page' => 4,
                'post_type' => 'cursos', 
                'orderby' => 'rand', 
                'post__not_in' => array($currentID) 
            ) 
        ); 
    ?>
    <?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?>
            // ESTRUTURA DO LOOP AQUI DENTRO
        <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

Note that I put an "orderby" as being "Rand", ie is taking the related posts randomly. Hence it is up to you.

I hope I’ve helped!

Thanks.

0

Thank you very much for the help, also managed to solve, in my tire had an error in the code 'post_not_in', the correct is 'post__not_in'follow the correct code if someone needs it:

Add in funcions.php

function postagem_relacionada() {

$post_id = get_the_ID();
$cat_ids = array();
$categories = get_the_category( $post_id );

if(!empty($categories) && is_wp_error($categories)):
    foreach ($categories as $category):
        array_push($cat_ids, $category->term_id);
    endforeach;
endif;

$current_post_type = get_post_type($post_id);
$query_args = array( 

    'category__in'   => $cat_ids,
    'post_type'      => $current_post_type,
    'post__not_in'    => array($post_id),
    'posts_per_page'  => '10'

 );

$related_cats_post = new WP_Query( $query_args );

if($related_cats_post->have_posts()):
     while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
       <div class="card text-gray height-100p shadow-v2">
          <a href="<?php the_permalink(); ?>">
            <img class="card-img-top" src="<?php echo get_field('imagem_do_curso') ?>" alt="">
          </a>
          <div class="p-4">
            <a href="<?php the_permalink(); ?>" class="h6">
              <?php the_title(); ?>
            </a>
            <button class="btn btn-primary btn-sm btn-pill" style="color: #191B31!important">
              + <?php echo get_field('quantidade_de_alunos_formados') ?> Alunos formados
            </button>
          </div>
        </div>   
    <?php endwhile;

    // Restore original Post Data
    wp_reset_postdata();
 endif;

}

Call on single.php or single-$Slug.php:

<?php postagem_relacionada() ?>

I hope this post will help someone else in the future, I thank everyone community, especially our friend Brício Fernandes who was willing to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.