Code Samples /

Exclude Posts

The code below will exclude posts that have been included in a previous query on the page.

<?php    
   // Standard WP Query.
   $args = array(
      'category_name' => 'wordpress',
      'posts_per_page' => 5
   );
   $query = new WP_Query($args);   
   while ( $query->have_posts() ) : $query->the_post();
      $do_not_duplicate[] = $post->ID; // Store ID in array.
      // ... Other loop actions could go here.
   endwhile; 
   wp_reset_query();   
   
   $post__not_in = $do_not_duplicate ? implode( ',', $do_not_duplicate ) : '';
   
   // Ajax Load More. 
   echo do_shortcode( '[ajax_load_more post__not_in="'. $post__not_in .'"]' );
PHP

The code below will exclude the current post from Ajax Load More results.

<?php
$post_id = get_the_ID(); // Current post ID
echo do_shortcode( '[ajax_load_more post__not_in="'. $post_id .'"]' );
?>
PHP

« Back to Code Samples