title: Wordpress Ajax pagination returning 0 after second time
body: <p>I am attempting to paginate my posts with an ajax.</p>
  
  <p>I set an attribute "data-page" to the page link and extract it with jQuery to pass it with Ajax.</p>
  
  <p>The first time a click on the link everything works fine but then if I click on a different link the ajax call returns 0.</p>
  
  <p>Here's my code in my functions.php:</p>
  
  <pre><code>    wp_register_script('load_post_ajax', get_template_directory_uri() .    '/includes/ajax/load_post_ajax.js',array( 'jquery' ), 1.1, true);
      $php_array = array( 'admin_ajax' =&gt; admin_url( 'admin-ajax.php' ) );
      wp_localize_script( 'load_post_ajax', 'php_array', $php_array );
      wp_enqueue_script( 'load_post_ajax' );
  </code></pre>
  
  <p>/*
   * PAGINATION
   */</p>
  
  <pre><code>function hs_pagination($pages = '', $range = 4)
  {
  $showitems = ($range * 2)+1;
  
  if(isset($_POST['paged'])){
      $paged = $_POST['paged'];
  }else{
      $paged = get_query_var( 'paged', 1 );
  }
  
  if(empty($paged)) $paged = 1;
  
  if($pages == '')
  {
      global $wp_query;
      $pages = $wp_query-&gt;max_num_pages;
      if(!$pages)
      {
          $pages = 1;
      }
  }
  if(1 != $pages)
  {
      echo "&lt;div class=\"pagination\"&gt;&lt;span&gt;Page ".$paged." of ".$pages." &lt;/span&gt;";
      if($paged &gt; 2 &amp;&amp; $paged &gt; $range+1 &amp;&amp; $showitems &lt; $pages) echo "&lt;a href='".get_pagenum_link(1)."' data-page=1&gt;&amp;laquo; First&lt;/a&gt;";
      if($paged &gt; 1 &amp;&amp; $showitems &lt; $pages) echo "&lt;a href='".get_pagenum_link($paged - 1)."'&gt;&amp;lsaquo; Previous&lt;/a&gt;";
  
      for ($i=1; $i &lt;= $pages; $i++)
      {
          if (1 != $pages &amp;&amp;( !($i &gt;= $paged+$range+1 || $i &lt;= $paged-$range-1) || $pages &lt;= $showitems ))
          {
              echo ($paged == $i)? "&lt;a href=\"#\" class=\"current\" data-page=$i&gt;".$i."&lt;/a&gt;":"&lt;a href='".get_pagenum_link($i)."' class=\"inactive\" data-page=$i&gt;".$i."&lt;/a&gt;";
          }
      }
  
      if ($paged &lt; $pages &amp;&amp; $showitems &lt; $pages) echo "&lt;a href=\"".get_pagenum_link($paged + 1)."\" data-page=$paged + 1&gt;Next &amp;rsaquo;&lt;/a&gt;";
      if ($paged &lt; $pages-1 &amp;&amp; $paged+$range-1 &lt; $pages &amp;&amp; $showitems &lt; $pages) echo "&lt;a href='".get_pagenum_link($pages)."' data-page=$pages&gt;Last &amp;raquo;&lt;/a&gt;";
      echo "&lt;/div&gt;\n";
  }
  </code></pre>
  
  <p>}</p>
  
  <p>/*
   * AJAX POSTS LOADER
   */</p>
  
  <pre><code>add_action( 'wp_ajax_load_post_ajax', 'load_post_ajax_init' );
  add_action( 'wp_ajax_nopriv_load_post_ajax', 'load_post_ajax_init' );
  function load_post_ajax_init() {
      $total_posts_per_page = get_option('posts_per_page');
      if($_POST['paged']){
         $paged = $_POST['paged'];
      }else{
         $paged = get_query_var( 'paged', 1 );
      }
  
      $args = array(
         'posts_per_page' =&gt; $total_posts_per_page,
         'paged' =&gt; $paged,
         'post_type' =&gt; 'post'
      );
      $posts = query_posts($args);
  
  if($posts){
      $count =0;
      foreach($posts as $post){
          ?&gt;
          &lt;article class="post"&gt;
              &lt;?php
              if($paged &lt; 2){
                  if(has_post_thumbnail($post-&gt;ID) ){
                      the_post_thumbnail($post-&gt;ID,'big',array('class'=&gt;'img-responsive center-block'));
                  }else{
                      echo "no thumb";
                  }
              }else{
                  echo "not first post";
              }
              ?&gt;
              &lt;p class="post_info hs_color"&gt;&lt;?php echo $post-&gt;post_title . " Posted on: " . $post-&gt;post_date . " Author: ". get_the_author_meta( 'display_name',$post-&gt;post_author );?&gt;&lt;/p&gt;
              &lt;p&gt;&lt;?php echo $post-&gt;post_content;?&gt;&lt;/p&gt;
              &lt;div class="post_link clearfix"&gt;
                  &lt;a href="&lt;?php the_permalink($post-&gt;ID);?&gt;"&gt;
                      &lt;h6 class="text-uppercase text-center"&gt;Read more&lt;/h6&gt;
                      &lt;div class="hs_square hs_square_right hs_bg"&gt;&lt;/div&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
          &lt;/article&gt;
          &lt;?php
          $count++;
      }
  }else {
      echo "no posts found";
  }
  
  //PAGINATION
  hs_pagination();
  //PAGINATION
  </code></pre>
  
  <p>}</p>
  
  <p>/*
  *AJAx CALL
  */</p>
  
  <pre><code>jQuery( document ).ready(function() {
  
   $('#post-section .container .pagination a').on('click',function(e){
      /** Prevent Default Behaviour */
      e.preventDefault();
  
      var page = $(this).attr('data-page');
  
      /** Ajax Call */
      $.ajax({
  
          cache: false,
          timeout: 8000,
          url: php_array.admin_ajax,
          type: "POST",
          data: ({
              action:'load_post_ajax',
              paged:page,
          }),
  
          beforeSend: function() {
          },
  
          success: function( data,response ){
              $( '#post-section .container' ).addClass('animated FadeIn');
              $( '#post-section .container' ).html( data);
          },
  
          error: function( jqXHR, textStatus, errorThrown ){
              console.log( 'The following error occured: ' + textStatus, errorThrown );
          },
  
      });
  
  
  });
  </code></pre>
  
  <p>});</p>