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.
Related Posts:
- This AJAX Code Doesn’t Work – Looking for elegant solution
- The correct way to call posts with ajax
- Two loops, one AJAX loop, exclude posts in first loop from second loop, loops are in different files
- Cant’ Grab WordPress Built-in Posts Through Loop
- WordPress Ajax load doesn’t work
- How to add an admin notice upon post save/update
- Print Current Post Index number within Loop
- Should we trust the post globals?
- How to get posts published between a date and today?
- How to make comments work for a post loaded per Ajax?
- Passing current cookies in wp_remote_get to get Draft Post Preview
- How set featured posts using checkbox in post edit screen?
- Get first post from wp_query
- Do we still need to include a “if (have_posts())” in templates?
- WP_Query: Why is sticky post not first item in loop?
- Ajax Load More Posts in Category Page
- Query *only* sticky posts
- Display all posts from specific categories on a page
- rewind_posts() – what actually the use of it, and where using is required or preferred?
- Skip post in loop and mixin later
- Listen to Post action
- How to show list of posts by author and category?
- How to load paginated post pages via ajax
- Getting the post_id in wp_ajax function
- Override the default number of posts to show for a single loop?
- get comments and get posts in loop
- How do I reorder (pop and push) items from WP_Query?
- How to exclude latest x posts from a paginated query?
- Manipulate post category after time
- Display content from a specific category
- Reverse chronology of post listing
- Can I load posts via Ajax?
- Why do I need to use The Loop on pages (inside page.php etc)?
- WooCommerce – Create Products Programatically [closed]
- Load posts dynamically
- posts page – different lengths of excerpt
- WordPress Number of Posts Not Changing With posts_per_page
- how to display full post with pagination on home page
- How to load post content on index page using ajax when post title in sidebar is clicked
- Count singular post views automatically
- Looping through posts per category gives same posts for each category
- How to use oEmbeds on Post Content during AJAX requests
- Find most recent authors
- Show Custom Taxonomy Categories, Listing of Posts, and Single Post via AJAX
- which action to hook to in order to perform post-publish action
- Display All Sticky Post Before Regular Post
- Set number of article per number of page
- Can’t show comments count per post outside loop
- Get the author of the latest revision
- How to change post status from publish to draft using hook in wordpress?
- How to have different content in the loop and single
- Specific loop in Shortcode
- Strange switch_to_blog() issue
- Show a list of recently viewed posts to a user
- Multiple loops in same page, without duplicate content
- loop not displaying posts on custom template page
- prevent showing posts of an specific category in admin posts section
- Repost post on specific date every year
- How to use alternate post layout at key points in post loop?
- Load post content into div with ajax
- POST from jQuery to PHP
- Alternate custom content in the loop
- Showing random content / pictures from earlier posts in a sticky post?
- Search widget breaks when using multiple loops?
- “Uncaught SyntaxError: Unexpected token
- How to Display Post View Count
- Displaying the number of posts in a custom loop (without including the number of posts of a loop below)?
- Using radio button meta data from a custom meta box
- Display Latest Post from all Categories
- WordPress theme ajax have_posts()
- Auto Refresh Post List after X seconds
- How do I use Ajax to show the new posts realtime, on the frontpage
- how load content as pop-up using ajax
- custom wordpress post loop – hide iframe content
- Double count view in archive.php
- Divide Loop Into Days & Categories
- Converting multiple loops into one single loop with pagination
- Display posts with comments closed, with pagination?
- Group posts by year in loop
- How would I get 1 latest post from a query for 5 posts?
- Load content in a div with ajax
- While in “the loop”, detect if a post is the most recent
- How to store an extra (surrogate) ID when creating a post with wp_insert_post?
- Alternating post layout
- Check to see if specific loop has less than certain amount of posts
- Different style for most recent article
- Contact form 7 post loop [closed]
- Differentiate between posts and pages in search results
- How to create a load more post entries like Twitter?
- AJAX filter posts by year
- Global $post not working in OOP function WordPress
- Related posts loop based on tags AND categories doesn’t work without at least one tag assigned
- Loop through all product posts?
- How to display different number of posts
- If Specific Post, Link Elsewhere
- How to filter posts that belong to a specific category only if that is the only category
- WP Query – duplicated posts once including tags in search results
- slideshow is not showing in a post call with ajax
- Most recent post from another database
- wp_tag_cloud() and the_taxonomies() work but not the_tag()