Getting the same post on my related post

I am displaying the related products on my single.php page.

I am getting two issues.

  1. When I am on a single page I am getting the same post on my related post.
  2. I have to show the max 4 post on my related section. If I have 3 posts related to the same category then display only 3

I tried the below code.

$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( 
        'taxonomy' => 'blogs_cat',
        'category__in'   => $cat_ids,
        'post_type'      => $current_post_type,
        'post_not_in'    => array($post_id),
        'posts_per_page'  => '4',
        'order' => 'DEC',
     );
    $related_cats_post = new WP_Query( $query_args );
    if($related_cats_post->have_posts()):
        echo '<div class="relatedPostWrapper"><ul>';
         while($related_cats_post->have_posts()): $related_cats_post->the_post();
          echo '<li>
                    <a href="'.get_permalink($related_cats_post->ID).'">
                        <div class="d-table">
                            <div class="relatedpost-img d-table-cell">
                            <img src="'.get_the_post_thumbnail_url($related_cats_post->ID).'">
                            </div>
                            <div class="relatedpost-title d-table-cell">
                                 '.get_the_title($related_cats_post->ID).'
                            </div>
                        </div>
                    </a>
                </li>';
        endwhile;
        echo '</ul></div>';
        wp_reset_postdata();
     endif;

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

        'post_not_in'    => array($post_id),

Here is your problem, and there are two of them:

  1. The parameter key is misspelt
  2. There’s a massive performance hit that can be easily avoided

Using the Correct Key

It should be post__not_in not post_not_in. This will restore the desired behaviour, but with a huge performance hit that gets heavier as the size of the posts table increases.

This is because you should always ask the database for what you want, never ask it for what you do not want. MySQL will respond to this by creating a brand new copy of the posts table that doesn’t have that post, then performing the query on the new table, then discarding it.

But there’s a simple alternative

Fixing the Performance

The solution is simple, ask for 5 posts instead of 4 and skip the post you don’t want. Excluding posts in PHP is almost instant in comparison.

For example:

$q = new WP_Query( [ 'posts_per_page' => 5 ] );
$counter = 0;
$post_to_skip = 1;
if ( $q->have_posts() ) {
    while ( $q->have_posts() {
        $q->the_post();
        if ( get_the_ID() === $post_to_skip ) {
            continue; // skip
        }
        if ( ++$counter === 4 ) {
            break; // only 4 posts
        }
        // ... display post
    }
    wp_reset_postdata();
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x