title: Using ajax to fetch products from wordpress database
body: <p>I am using WordPress to build a custom products page (page1.php).</p>
  
  <p>I'm using Ajax on the custom products page(page1.php), to call an other page that contains the code below (page2.php), to fetch products from the wordpress database using the code below. </p>
  
  <pre><code>&lt;?php  
      $args = array(
          'post_type'      =&gt; 'product',
          'posts_per_page' =&gt; 10,
          'product_cat'    =&gt; 'hoodies'
      );
  
      $loop = new WP_Query( $args );
  
      while ( $loop-&gt;have_posts() ) : $loop-&gt;the_post();
          global $product;
          echo '&lt;br /&gt;&lt;a&gt;' . woocommerce_get_product_thumbnail().' '.get_the_title().'&lt;/a&gt;';
      endwhile;
  
      wp_reset_query();
  ?&gt;
  </code></pre>
  
  <p>The code above actually works fine when I dont call it through AJAX (i.e. load it directly from www.localhost/wordpress/page2.php), but when I call it through ajax on page1.php, I get the following error;</p>
  
  <p>Fatal error: Uncaught Error: Class '<strong>WP_Query</strong>' not found in C:\xampp\htdocs\wordpress-fully-custom\wp-content\themes\storefront\page2.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\wordpress-fully-custom\wp-content\themes\storefront\test-page2.php on line 9</p>
  
  <p>how do I fix this please?</p>
  
  <p>Thanks</p>