the_title() and the_permalink() won’t work on AJAX calls

I’ve run into a strange problem.

I have a custom loop built with get_posts that works fine when loading the page normally:

<?php
    $rows = get_posts(array(
        'post_type'     => 'drinks', 
        'numberposts'   => -1
    ));
?>

<?php foreach ($rows as $post) : setup_postdata($post) ?>
    <?php the_post_thumbnail() ?>
    <h3><?php the_title() ?></h3>
    <?php the_content() ?>
    <?php the_permalink() ?>
<?php endforeach; wp_reset_postdata() ?>

That code sits inside its own template called “drinks.php”.

Now I’ve set up an AJAX function/URL to fetch this template using jQuery:

add_action('wp_ajax_h5b_get_user_drinks', 'h5b_ajax_get_user_drinks');
add_action('wp_ajax_nopriv_h5b_get_user_drinks', 'h5b_ajax_get_user_drinks');

function h5b_ajax_get_user_drinks () {
    include 'drinks.php';
    die;
}

However, when I fetch it with AJAX it seems that neither the_title(), the_post_thumbnail() or the_permalink() work. the_content() seems to work fine though.

If I var_dump($post) inside my loop it has all the data it should (like title, guid etc).

How can this be?

2 Answers
2

Try this:

function h5b_ajax_get_user_drinks() {
    return get_template_part( 'drinks' );

    exit;
}

Leave a Comment