I am passing variable with ajax to list.php in my twentytwelve template. In list.php I am executing mysql select query but when I see in console I am getting this error:

<br />
<b>Fatal error</b>:  Call to a member function get_results() on a non-object in <b>D:\xampp\htdocs\wordpress\wp-content\themes\twentytwelve\list.php</b> on line <b>4</b><br />

My Code:


Template Name: Search

<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.min.js"></script>
<script type="text/javascript">
    var parent_category = $(this).val();
    if(parent_category != '')  
         type: "POST",
         url: "<?php echo get_template_directory_uri(); ?>/list.php",
         data: "parent_category="+ parent_category,
         success: function(option)
       $("#child_category").html("<option value=''>-- No category selected --</option>");
    return false;

<select id="parent_category" name="parent_category">
    <option value="" selected="selected">-- Select blood group --</option>
    <option value="A1 positive">A1 positive</option>
    <option value="A1 negative">A1 negative</option>
    <option value="A2 positive">A2 positive</option>
    <option value="A2 negative">A2 negative</option>
    <option value="B positive">B positive</option>
    <option value="B negative">B negative</option>
    <option value="A1B positive">A1B positive</option>
    <option value="A1B negative">A1B negative</option>
    <option value="A2B positive">A2B positive</option>
    <option value="A2B negative">A2B negative</option>
    <option value="AB positive">AB positive</option>
    <option value="AB negative">AB negative</option>
    <option value="O positive">O positive</option>
    <option value="O negative">O negative</option>
    <option value="A positive">A positive</option>
    <option value="A negative">A negative</option>

<select id="child_category" name="child_category">
  <option value="">-- No location selected --</option>

<?php get_footer(); ?>


if(isset($_POST['parent_category']) && $_POST['parent_category'] != '')
    $result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );

Any ideas or suggestions? Thanks.

1 Answer 1


Your list.php file does not load any of the WordPress Core, at least there is no indication that it does based on the code you posted. Thus, WordPress classes and functions are not going to be available, hence the error.

Merely having a file in the theme folder does not mean that WordPress will load the file, or that when accessed the file will be loaded in a WordPress context.

Do the right thing and use the AJAX API for AJAX requests. That is what it was meant for. You are doing this the hard way, and the painful way. There are plenty of examples in the Codex for using the AJAX API and plenty of questions here about it as well. Get started. If you have trouble, edit your question with the specifics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.