I have a Wordpress AJAX call that is working in as much as it is able to get raw data from a PHP file. The AJAX is using the admin-ajax.php file. However, the PHP file I need to get is within the loop of the page. Where it is being placed is where it works if it was just included on the page.

To summarize: if the PHP file being AJAXed has in it:

<?php echo 'this is the file!' ?>

…everything works fine. But if it has:

<?php the_title(); ?>

…it returns blank.

Thank you for taking the time to read this!

1 Answer 1


There is no such thing as an AJAX call that's inside the loop. The server has no idea what page or where on that page an ajax call is coming from unless you explicitly send it that data as a part of the ajax call. You're going to need to send some data to the php function so it can recreate the correct query (or the current post if that's all you need.) i.e. You send a post ID in the ajax, the php function it calls then does $post_id = $_POST[post_id]; (please add your own sanitization and validation here.) Then instead of calling the_title() you would call get_the_title($post_id)

  • Thank you so much. That really helps clear it up. And thanks for bearing with me as I am just learning AJAX within Wordpress. I have the post id in a data attr on the button being clicked that is firing the AJAX, how do I get that post id within the AJAX data?
    – jons
    Commented Jun 19, 2019 at 16:28
  • post your javascript that fires the ajax call and I'll tell you what changes you need
    – mrben522
    Commented Jun 19, 2019 at 17:51

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.