title: How to use get_posts() function in functions.php
body: <p>I am trying to load posts with ajax. But I am not getting any content of the posts. I am not posting my javascript since it is pretty straight.
  Here is my code in inc/helper-functions.php which is included in main functions.php.</p>
  
  <pre><code>function get_my_posts(){
  
      $posts_per_page = $_POST['posts_per_page'];
      $category = $_POST['category'];
  
      $args = array(
          'posts_per_page'   =&gt; $posts_per_page,
          'offset'           =&gt; 0,
          'category'         =&gt; $category,
          'orderby'          =&gt; 'date',
          'order'            =&gt; 'DESC'
      );
      $myposts = get_posts( $args );
  
      if ( $myposts ) {
  
          foreach ( $myposts as $post ) {
              setup_postdata( $post );
              get_template_part( 'content', get_post_format() );
          }
          wp_reset_postdata();
  
      } else {
          echo 'Ingen innhold';
      }
  
      wp_die();
  }
  
  add_action("wp_ajax_get_my_posts", "get_my_posts");
  add_action('wp_ajax_nopriv_get_my_posts', 'get_my_posts');
  </code></pre>
  
  <p>Here is how content.php looks like:</p>
  
  <pre><code>&lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt;&gt;
  
      &lt;div class="meta"&gt;
          &lt;?php the_time('d.m.Y') ?&gt;
      &lt;/div&gt;
  
      &lt;header class="entry-header"&gt;
          &lt;?php  the_title( sprintf( '&lt;h2 class="entry-title"&gt;&lt;a href="%s" rel="bookmark"&gt;', esc_url( get_permalink() ) ), '&lt;/a&gt;&lt;/h2&gt;' ); ?&gt;
  
      &lt;/header&gt;
  
      &lt;div class="entry-content"&gt;
          &lt;?php the_excerpt(); ?&gt;
      &lt;/div&gt;
  
  &lt;/article&gt;
  </code></pre>
  
  <p>I am getting through the loop, but don't get the contents to be printed. None of the functions for ex. the_ID(), the_excerpt(),  post_class(), etc. don't work. If I use the same code in the template, it works fine.</p>