-1

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>

see the example of problem on the image

0

2 Answers 2

2

Your 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!

4
  • thx for answer but it seems this code break down my website . this code is very heavy for website. Commented Apr 21, 2019 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. Commented Apr 21, 2019 at 11:48
  • An other solution is to delete each duplicate post after the plugin publish the posts from RSS feed. Commented Apr 21, 2019 at 11:50
  • 1
    thanks. I changed -1 to 20 and now everything is ok Commented Apr 21, 2019 at 14:18
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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