The idea is simple. I have a drop-down menu generated with get_categories(). This outputs a list of categories in an unordered list, each in it's own li element.
I'm getting the text values of those li elements when a user clicks them, and based on that i want to create a custom wp_query, that returns posts from that category only. To do this, i`m loading the text value of the list element into a JS variable, send it over to a php file for processing. That php file constructs the string like so:

$ff_query = new WP_Query('posts_per_page=2&category_name='.$_POST['JSvariable']); 

… and ideally should execute it, returning the info from the DB. The error i get is:

"Fatal error: Class 'WP_Query' not found in C:\xampp\htdocs\suply\wp-content\themes\suply\includes\proc_cat.php on line 3"

This is what my 2 files look like:

1) file that sends the ajax request:

    $(document).ready(function(e) {
        $("#ddmenu li").on('click', function() {
            var cValue = $(this).text();
            $.post("<?php bloginfo('template_url');?>/includes/proc_cat.php", {name: cValue}, function(cat){                
            });//end of post
        });//end of click
    });//end of ready

2) file that should process the

        $name = 'posts_per_page=2&category_name='.$_POST['name'];
        $ff_query = new WP_Query('posts_per_page=2&category_name='.$_POST['name']);
      something else, but it won`t reach this part.

What can i do to make this work? Or as an alternative, how can i create the type of functionality i want? (basically, a different query, generated dynamically, based on a user selection).

1 Answer 1


That's because when calling a theme template directly you only have what's included/defined in the template file, not the whole WordPress environment.

Some people attempt to fix this by adding an include of wp-header or load etc but this is incredibly damaging

When doing AJAX requests, never call a file in the theme, instead call the WP AJAX APIs.

With the WP AJAX API your file becomes a simple function in functions.php.

I strongly suggest you follow the advice of this article:


e.g. PHP:

wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) )
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' );
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' );

function myajax_submit() {
    // get the submitted parameters
    $postID = $_POST['postID'];

    // generate the response
    $response = json_encode( array( 'success' => true ) );

    // response output
    header( "Content-Type: application/json" );
    echo $response;

    // IMPORTANT: don't forget to "exit"


    // see tip #1 for how we declare global javascript variables
        // here we declare the parameters to send along with the request
        // this means the following action hooks will be fired:
        // wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
        action : 'myajax-submit',

        // other parameters can be added along with "action"
        postID : MyAjax.postID
    function( response ) {
        alert( response );
  • Thank you for resonding so quickly :) I stumbled upon that article as well, but didnt understand much, since im very new at both WP and AJAX, but i will give it a go, and hopefully come to a result. Just to be on the safe side though, am i on the right track trying to implement this using WP AJAX APIs (or AJAX for that matter)? Commented Oct 4, 2012 at 23:36
  • For listing categories in a dropdown menu? Probably not worth bothering with AJAX, if there's a lot of them then there'll be too many to be navigable, so use autocomplete scripts instead
    – Tom J Nowell
    Commented Oct 5, 2012 at 9:01
  • You didnt understand what i wanted. My needs are: when a user clicks on an element of the drop down, a custom query is created based on his selection. I cant have a predetermined list of selectable elements, since that list is dynamically generated when the admin creates his categories. Commented Oct 6, 2012 at 18:59
  • Then ignore my comment and pay attention to my answer instead for the correct and reliable usage of AJAX in WordPress
    – Tom J Nowell
    Commented Oct 7, 2012 at 13:37
  • Hiya, the website you linked to seems to have been removed; do you think you could find a similar article elsewhere or add a description to your article? Commented Aug 10, 2017 at 21:43

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.