How to wp_query posts with ajax

I would like to make a series of comments, but the overall length would be too long for a comment, so I have to write an answer.

  1. You should state what is working and what is failing, and tell us what you have tried to get it working. Looking at your code is too general.

  2. In wordpress, the jQuery alias $ generally does not work. However, if you have the Developer Tools console open, you should get an error message. I always use jQuery instead of $ just to avoid stupid errors.

  3. You state you would like to hover over a category, but your trigger is on click.

    $( "#alabama" ).click(function() {

    If you are not getting any hover behavior, try clicking.

  4. Your AJAX call looks all right, though the call back function seems to be putting the data in the wrong location. If you want the query to happen on a hover, shouldn’t the output go into a tooltip?

  5. The action in the AJAX call is to fetch_posts but your PHP function is called import_post. These two functions need to be identical for AJAX to work.

  6. Your PHP function uses the $_REQUEST[] variable. Since you are sending the AJAX via POST, then you can use the $_POST[] variable. It probably won’t break or fix anything to change, but to me it makes more sense (the code is more readable).

  7. When I develop an AJAX call, I set up the jQuery AJAX call, like you have. Then I create the PHP function that handles the AJAX call. First, I just echo the data I sent via AJAX. Once I know that works, then I write the SQL, echo that to make sure there are no syntax errors. At that point, I grab the data I want and send it back. The last step is to properly display the data in the AJAX callback function.

OK, there’s your AJAX tutorial. Hope it helps.