Cannot access current post’s ID in custom plugin

As you can see, I tried declaring the $post variable as global to account for failures due to being outside of the Loop.

The problem is not that the post is “outside of the loop”, problem is that AJAX request is a completely separate HTTP request.

When you do an AJAX request, is just like you are opening a new window on browser and open the url in this separate window. It means that the script that handles the AJAX request knows nothing about the page that sent the request.

If you need to process a specific post in the AJAX request, you need to send the post ID to process as part of the AJAX request data.

Leave a Comment