Title: Custom loop and duplicate post
Last modified: August 30, 2016

---

# Custom loop and duplicate post

 *  [devolution](https://wordpress.org/support/users/devolution/)
 * (@devolution)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/custom-loop-and-duplicate-post/)
 * Hi, i’ve sort-of written a loop to get some pagination going on, but whenever
   the loop reaches the end, the last post will be repeated (if it’s not an odd 
   post count).
 * The “pagination” system is a basic ajax query:
 *     ```
       <?php 
   
       function charger_articles() {
   
       	$offset = $_POST['offset'];
   
       	$args = array(
       		'category_name'=>get_the_title(),
       	    'post_type' => 'post',
       	    'offset' => $offset,
       	    'posts_per_page' => '2',
       	);
   
       	$ajax_query = new WP_Query($args);
   
       	//var_dump($ajax_query);
   
       	if ( $ajax_query->have_posts() )
       	{
   
       		while ( $ajax_query->have_posts() )
       		{
       			$ajax_query->the_post();
       					echo '<div class=\'categorieMiniArticle\'>';
       						echo '<div class=\'col-xs-12 col-md-6\' max_height=\'300px\'>';
       							echo '<div class=\'miniatureContainer\'>';
       								he_post_thumbnail( 'moitie',array(
       													'class' => 'img-responsive',
   
       													));
       							echo '</div>';
       							echo '<div class=\'miniArticlesContainer\'>';
       								echo '<a>';
       									echo '<h2>' . get_the_title() . '</h2>';
       									echo '<h3>' . get_the_excerpt() . '</h3>';
       								echo '</a>';
       							echo '</div>';
       						echo '</div>';
       					echo '</div>'; 
   
       		}
       		echo '<div class=\'moreposthere\'></div>';
   
       	}
   
       	die();
   
       }
   
       ?>
       ```
   
 * And the ajax request:
 *     ```
       var offset = 3;
       jQuery('body').on('click', '.load_more', function()
       {
   
       	jQuery.post(
       	    ajaxurl,
       	    {
       	        'action': 'charger_articles',
       	        'offset': offset
       	    },
       	    function(response)
       	    {
       	    	if (response)
       	    	{
   
       		    	offset= offset + 2;
       		    	// console.log(response);
       		    	jQuery( ".moreposthere" ).append(response);
   
       	    	}
       	    }
       	);
       });
       ```
   
 * offset is set to 3 because my page has 3 articles shown before the ajax request
   can be made, and than i want it to add two articles per click on the button. 
   It does work, but when it reaches a point where it can only find one, cause offset
   = numberOfArticles – 1; it will add twice the same article
 * Can someone provide some help?

The topic ‘Custom loop and duplicate post’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [devolution](https://wordpress.org/support/users/devolution/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/custom-loop-and-duplicate-post/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
