title: Efficiency with php code header
body: <p>This code isn't optimal and I am struggling to find a better way of doing it.</p>
  
  <p>Basically, you click a button which sends an ajax request to this file listed below which gets a random post from a WordPress database. Quite often these are repeated and gives the impression that it is not working, to combat this I checked if current id = old id, if they are the same get a new post, however I have not found a way to run another wpquery inside of another query.</p>
  
  <pre><code>&lt;?php
      require_once('../../../wp-blog-header.php');
      header('HTTP/1.1 200 OK');
  ?&gt;
      &lt;span id="postss"&gt;&lt;?php
          query_posts(array(
              'cat' =&gt; 39,
              'order' =&gt; 'ASC', // ASC
              'orderby' =&gt; 'rand',
              'showposts' =&gt; 1,
              ));
          $wp_query-&gt;is_archive = true; $wp_query-&gt;is_home = false;
          if (have_posts()) : while (have_posts()) : the_post();
  
          session_start();
          if(!isset($_SESSION['oldId']))
          {
              $_SESSION['oldId'] = get_the_id();
          }else{
              $curId = get_the_id();
              if($_SESSION['oldId'] == $curId)
              {
                  header("Location: http://website.com/testimonialPull.php");     
              }else{
  
  
      the_content();
          }
          $_SESSION['oldId'] = get_the_id();
      }
  
      endwhile; endif;
  
  ?&gt;
  </code></pre>
  
  <p>So sending the header request takes about 1 seconds for a new post to show, whereas normally its about 2/10ths of a second. Is there a more efficient and faster way of doing this?</p>