WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For a theme I'm building I'm looking to dynamically load in a selection of posts with Ajax depending on if a user selects a category or a year ('show all posts in 2012').

I've seen many different approaches described on how to implement Ajax in WordPress but they all do one aspect (like infinite scroll for instance). I was wonder if there is one integrated solution that allows to give a variable to the loop via Ajax and a specific selection of posts is returned?

share|improve this question

You can easily add whatever variables you want to your AJAX call.

Using jQuery you can set a AJAX action like this:

        action: 'your_called_function',
        argument: 'year' or 'category',
        some_other_var: 'some value'
    function( response ) {
        if(response) {
        } else {

Then in your functions.php add this:

add_action( 'wp_ajax_nopriv_your_called_function', 'your_called_function' );
add_action( 'wp_ajax_your_called_function', 'your_called_function' );
function your_called_function() {
    $argument = $_POST['argument'];
    $otherVar = $_POST['some_other_var'];
    // now you can do the loop with your argument

    // important to add exit(), or it won't work

In detail here: http://www.makeuseof.com/tag/tutorial-ajax-wordpress/

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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