title: Load more posts in the same category - WordPress + Ajax + Timber
body: <p>I use Timber with WordPress.</p>
  <p>I'm trying to create a list of posts with a <code>&quot;Load more posts&quot;</code> button.</p>
  <p>I would like to display 10 posts of the same category and to load 10 others posts of the same category when the user click on the button <code>&quot;Load more posts&quot;</code></p>
  <p>All work fine when there is no distinction between categories. The button <code>&quot;Load more posts&quot;</code> work fine. 10 posts are display.</p>
  <p>But when I try to display posts in the same category when I click on the button <code>&quot;Load more posts&quot;</code>. No post are display. What's wrong with category ?</p>
  <p>Can I have some help please ?</p>
  <h2>archive.php</h2>
  <pre><code>$category = get_the_category($post-&gt;ID);
  $category = $category[0]-&gt;cat_ID;
  
  $context['posts'] = Timber::get_posts(array(
      'post_type' =&gt; 'post',
      'post_status' =&gt; 'publish',
      'category__in' =&gt; array($category),
      'posts_per_page' =&gt; 10,
      'paged' =&gt; 1,
      'has_password' =&gt; FALSE
  ));
  </code></pre>
  <h2>script.js</h2>
  <pre><code>function load_more_news() {
      var page = 1;
  
      $(document).on('click', '#load-more-news', function(e) {
          e.preventDefault();
          page++;
  
          $.ajax({
              type: 'POST',
              url: '/wp-admin/admin-ajax.php',
              dataType: 'html',
              data: {
                  'action' : 'get_news',
                  'get_page' : page
              },
              success: function(data) {
                  if($('&lt;div&gt;&lt;/div&gt;').html(data).find('.archive__item.ended').size() &gt; 0) $('#load-more-news').parents('.cta').remove();
                  else $('#load-more-news').parents('.cta').show();
                  $('#archive__list').append(data);
              },
              error: function(data) {
                  console.log(data);
              }
          });
      });
  }
  </code></pre>
  <h2>functions.php</h2>
  <pre><code>add_action( 'wp_ajax_nopriv_get_news', 'get_news' );
  add_action( 'wp_ajax_get_news', 'get_news' );
  function get_news() {
      global $post;
  
      $context = Timber::get_context();
      $context['get_page'] = empty($_POST['get_page']) ? 1 : $_POST['get_page'];
  
      $category = get_the_category($post-&gt;ID);
      $category = $category[0]-&gt;cat_ID;
  
      $context['posts'] = Timber::get_posts(array(
          'post_type' =&gt; 'post',
          'post_status' =&gt; 'publish',
          'category__in' =&gt; array($category),
          'posts_per_page' =&gt; 10,
          'paged' =&gt; $context['get_page'],
          'has_password' =&gt; FALSE
      ));
      $count = count(Timber::get_posts(array(
          'post_type' =&gt; 'post',
          'posts_per_page' =&gt; -1,
          'post_status' =&gt; 'publish',
          'category__in' =&gt; array($category)
      )));
  
      if($count &lt;= $context['get_page'] * 10) $context['ended'] = 'ended';
  
      Timber::render( 'bloc_news.twig', $context );
  
      die();
  }
  </code></pre>
  <h2>archive.twig</h2>
  <pre><code>&lt;section class=&quot;archive&quot;&gt;
      &lt;p class=&quot;archive__title h-headline text-size-l&quot;&gt;In the same thematic&lt;/p&gt;
      &lt;div id=&quot;archive__list&quot; class=&quot;archive__list&quot;&gt;
          &lt;article class=&quot;post&quot;&gt;
              ...
          &lt;/article&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  
  {% if posts|length &gt;= 10 %}
  &lt;section class=&quot;cta&quot;&gt;
      &lt;a href=&quot;#&quot; id=&quot;load-more-news&quot;&gt;
          &lt;p class=&quot;cta__text&quot;&gt;Load more posts&lt;/p&gt;
      &lt;/a&gt;
  &lt;/section&gt;
  {% endif %}
  </code></pre>