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

                    $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();
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
<?php endwhile; endif; wp_reset_query(); ?>

see the example of problem on the image


Your code should look like this to exclude duplicate titles

    // 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 ) ) {
                    <a href="<?php the_permalink(); ?>" target="_blank">
                        <?php the_title(); ?>
                // Mark current title as displayed by adding it to the titles array.
                $titles[] = get_the_title();
                // Increase counter.


            // When we have displayed 10 posts the loop should stop.
            if ( $counter == 10 ) {

        endwhile; endif;
    wp_reset_query(); ?>

I hope this may help!

  • thx for answer but it seems this code break down my website . this code is very heavy for website. Apr 21 '19 at 11:18
  • 1
    If the code is heavy for website, then assuming every post is published twice we can safely use 'posts_per_page' => 20 instead of 'posts_per_page' => -1. This will reduce the number of posts fetched from db significantly. Apr 21 '19 at 11:48
  • An other solution is to delete each duplicate post after the plugin publish the posts from RSS feed. Apr 21 '19 at 11:50
  • 1
    thanks. I changed -1 to 20 and now everything is ok Apr 21 '19 at 14:18

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);
  • can you plz complete the main code with this structure?! Apr 21 '19 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.