Wordpress Tips and Tricks : Avoid Duplicate Posts


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /* Start non-duplicates */
  3. function post_strip($where) {
  4. global $myPosts, $wpdb;
  5. $where .= " AND $wpdb->posts.ID not in($myPosts) ";
  6. return $where;
  7. }
  8. ?>
  9. <?php
  10. global $myPosts;
  11. $myPosts = '';
  12. ?>
  13. <div>
  14. <?php
  15. $my_query = new WP_Query();
  16. $my_query->query('showposts=5');
  17. if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
  18. <?php $myPosts .= $post->ID . ","; ?>
  19. <div><?php the_title(); ?></div>
  20. <!-- Post Stuff -->
  21. <?php endwhile; endif; ?>
  22. </div>
  23. <?php $myPosts .= "0" ?>
  24. <?php add_filter('posts_where', 'post_strip'); ?>
  25. <div><br/>
  26. <?php //$my_query = new WP_Query('category_name=Life&showposts=15');
  27. $my_query = new WP_Query('showposts=5');
  28. while ($my_query->have_posts()) : $my_query->the_post();?>
  29. <div><?php the_title(); ?></div>
  30.  
  31. <!-- Post Stuff -->
  32.  
  33. <?php endwhile; ?>
  34. </div>
  35. <?php remove_filter('posts_where', 'post_strip'); ?>
  36. <?php /* End non-duplicates */ ?>

URL: http://w3updates.com/wordpress-tips-and-tricks-avoid-duplicate-posts/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.