How do I filter posts in WordPress and return the query using Ajax?

Andrea_AndMore April 18, 2013
Pinterest Stumbleupon Whatsapp

Hi there, I’ve read the amazing post by James Bruce A Tutorial On Using AJAX In WordPress A Tutorial On Using AJAX In WordPress AJAX is a remarkable web technology that moved us beyond the simple “click link, go to another page” structure of The Internet 1.0. It enables websites to dynamically fetch and display content without the user... Read More the WordPress guy but I am just wondering why James used a hidden field in the form to call the PHP function.

How is this working? For a project, I have to provide some filters to customize the post query and I would like to return the result via Ajax. With this tutorial I am getting close to the solution but I would like to understand what is going on with that hidden field since I did not find any documentation about it…

Thanks guys!

  1. James Bruce
    April 19, 2013 at 7:04 am

    Hi Andrea. I guess I should Answer this ;) It's been a year since I wrote that, but I believe it's because the form action would normally be a URL of the PHP to process the file - in this case, we don't want that (unless you want to deal with users who don't have javascript enabled), so I left it blank. The hidden form field called "action" - though you could call it anything really, "task" or "functionToCall" - is simply to tell wordpress which function we want to process the form. This allows to have multiple functions for processing different forms, all going via WP.

    If I were to program the same thing nowadays, I would actually bypass the complications of WordPress and just write my own PHP AJAX handler instead. The method outlined in this post would only be preferred if I were making a plugin for public distribution; on MakeUseOf for instance, we don't do that.