admin管理员组文章数量:1321240
I am displaying the related products on my single.php page.
I am getting two issues.
- When I am on a single page I am getting the same post on my related post.
- 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;
I am displaying the related products on my single.php page.
I am getting two issues.
- When I am on a single page I am getting the same post on my related post.
- 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;
Share
Improve this question
edited Sep 27, 2020 at 16:56
user9437856
asked Sep 27, 2020 at 16:32
user9437856user9437856
1117 bronze badges
1 Answer
Reset to default 0 'post_not_in' => array($post_id),
Here is your problem, and there are two of them:
- The parameter key is misspelt
- 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();
}
本文标签: pluginsGetting the same post on my related post
版权声明:本文标题:plugins - Getting the same post on my related post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742097785a2420656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论