admin管理员组文章数量:1431720
I am using a automatic rss post reader on my website and there is a problem with this plugin that sometimes publish duplicate posts.I can't do anything to this plugin but at least it is better if I could prevent to display the same titles on main page. here is my code
<ul>
<?php
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
if($portfolio->have_posts()) :
while($portfolio->have_posts()) : $portfolio->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
</li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
I am using a automatic rss post reader on my website and there is a problem with this plugin that sometimes publish duplicate posts.I can't do anything to this plugin but at least it is better if I could prevent to display the same titles on main page. here is my code
<ul>
<?php
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
if($portfolio->have_posts()) :
while($portfolio->have_posts()) : $portfolio->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
</li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
Share
Improve this question
asked Apr 21, 2019 at 5:40
m.javad koushkim.javad koushki
54 bronze badges
0
2 Answers
Reset to default 2Your code should look like this to exclude duplicate titles
<ul>
<?php
// Initial counter for displayed posts.
$counter = 1;
// Initial empty array of displayed titles.
$titles = [];
$portfolio = new WP_Query([
'post_status' => 'publish',
'post_type' => 'post',
'cat' => '' . $link1 . '',
// Because we don't know the number of duplicate titles, fetch all.
'posts_per_page' => -1,
]);
if ( $portfolio->have_posts() ) :
// Start the loop.
while ( $portfolio->have_posts() ) : $portfolio->the_post();
// If the current title has not been displayed already, display it.
if ( ! in_array( get_the_title(), $titles ) ) {
?>
<li>
<a href="<?php the_permalink(); ?>" target="_blank">
<?php the_title(); ?>
</a>
</li>
<?php
// Mark current title as displayed by adding it to the titles array.
$titles[] = get_the_title();
// Increase counter.
$counter++;
}
// When we have displayed 10 posts the loop should stop.
if ( $counter == 10 ) {
break;
}
endwhile; endif;
wp_reset_query(); ?>
</ul>
I hope this may help!
You somehow have to get hold of the post IDs you want to exclude. By calling the plugin's WP_Query
again for example and at best let you return only post IDs by 'fields' => 'ids'
this time.
Then you can use the result to exclude these posts from your other queries by passing an array of post IDs to 'post__not_in'
. So let's assume you created an array of the posts you want to exclude that looks like that $exlcude = [ 123, 456, 789 ]
then the query needs to look like this:
$args = array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' => $link1,
'posts_per_page' => '9',
);
if ( ! empty( $exclude ) ) {
$args['post__not_in'] = $exclude;
}
new WP_Query($args);
本文标签: query postsprevent display duplicate titles on main page
版权声明:本文标题:query posts - prevent display duplicate titles on main page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745552658a2663014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论