title: Ajax in Wordpress to reload a SELECT every second
body: <p>My target is to take a page of an existent WP theme and customize a full width page, to make that every 1 seconds a MySQL SELECT query is reloaded and the results are shown.</p>
  
  <p>Starting with <a href="https://github.com/stafmans/stafmans.wiki.wordpress/wiki/6.3-AJAX-content-refresh-Wordpress" rel="nofollow noreferrer">this</a> snippets I have made the following editing/new files:</p>
  
  <ol>
  <li>template-fullwidth.php</li>
  <li>functions.php</li>
  <li>my.js (new file)</li>
  <li>GetPostDate.php (new file)</li>
  </ol>
  
  <p><strong>What I have done</strong></p>
  
  <p>I started from the <code>template-fullwidth.php</code> of a theme and add the following code between the <code>DIV</code> of the post. The place where I wanted to show the results is between <code>&lt;div id="MaxPostDate"&gt;</code> tags.</p>
  
  <pre><code>&lt;/div&gt;&lt;!-- .post --&gt;
  &lt;!-- START CUSTOM PAGE CODE --&gt;
  &lt;?php
      if ( is_user_logged_in() ) {
      // --- START IF USER LOGGED IN --- //
          // Get the current user name and id
  ?&gt;
          &lt;div id="MaxPostDate"&gt;&lt;/div&gt;
          &lt;?php      
  
  
  
      // --- END IF USER LOGGED IN --- //
      } else {
      // --- START IF USER NOT LOGGED IN --- //
      echo "&lt;p&gt;You have to log-in to see this content&lt;/p&gt;";
      // --- END IF USER NOT LOGGED IN --- //
      } 
  ?&gt;
  
                      &lt;div class="result"&gt;&lt;/div&gt;
  &lt;!-- END CUSTOM PAGE CODE --&gt;
                      &lt;?php comments_template( '', true ); ?&gt;
  
                  &lt;/div&gt;&lt;!-- .posts --&gt;
  </code></pre>
  
  <p>then I have edited the <code>functions.php</code> file of the theme adding this in the end:</p>
  
  <pre><code>function add_myjavascript(){
    wp_enqueue_script( 'ajax-implementation.js', get_bloginfo('template_directory') . "/js/my.js", array( 'jquery' ) );
  }
  add_action( 'init', 'add_myjavascript' );
  
  function MyAjaxFunction(){
    //get the data from ajax() call
  
      $TableContent = $wpdb-&gt;get_results("
              SELECT MAX(`post_date`) AS MaxDate FROM wp_posts"
      );
      foreach($TableContent as $Content){echo "&lt;h1&gt;" . $Content-&gt;MaxDate . "&lt;/h1&gt;";}
  
    // Return the String
     die($results);
    }
    // creating Ajax call for WordPress
     add_action( 'wp_ajax_nopriv_MyAjaxFunction', 'MyAjaxFunction' );
     add_action( 'wp_ajax_MyAjaxFunction', 'MyAjaxFunction' );
  
  
  ?&gt;
  </code></pre>
  
  <p>Then, I have made this <code>my.js</code> in the <code>js</code> folder of the theme:</p>
  
  <pre><code>jQuery(document).ready(function refresh_div() {
          jQuery.ajax({
              url:'/MySite/wp-content/themes/hemingway/GetPostDate.php',
              type:'POST',
              success:function(results) {
                  jQuery(".result").html(results);
              }
          });
      }
  
      t = setInterval(refresh_div,1000););
  </code></pre>
  
  <p>And finally make another file GetPostDate.php</p>
  
  <pre><code>&lt;?php
  $TableContent = $wpdb-&gt;get_results("
              SELECT MAX(`post_date`) AS MaxDate FROM wp_posts"
      );
      foreach($TableContent as $Content){echo "&lt;h1&gt;" . $Content-&gt;MaxDate . "&lt;/h1&gt;";
  
                                         ?&gt;
  </code></pre>
  
  <p><strong>The problems</strong></p>
  
  <ul>
  <li>nothing appear inside the DIV id="MaxPostDate"</li>
  <li>I have wrote two time the same query (<code>SELECT MAX(</code>post_date<code>) AS MaxDate FROM wp_posts</code>), I would be wrote just one time! </li>
  </ul>