I’m working on implementing a “Show More Articles” functionality using AJAX calls to fetch additional content from my WordPress site. My goal is to load 4 posts that have a specific tag, but for some reason the AJAX response only contains one post instead of the expected four posts.
Your loop’s probably outputting HTML but getting cut short somewhere. Instead of fighting WordPress quirks, just automate the whole thing.
I’ve dealt with similar content loading issues by setting up automated workflows that pull from WordPress APIs and return clean JSON. Ditches the messy PHP file approach completely.
Build a workflow that connects to your WordPress REST API, filters posts by category, formats everything exactly how you want, and serves it through a clean endpoint. No more wrestling with wp_reset_postdata() or query conflicts.
It handles pagination automatically and you can cache responses for better performance. Plus you get built-in error handling and logging, so you’ll actually know what’s breaking.
Makes your frontend code cleaner too since you’re just fetching JSON instead of loading raw HTML.
check the console for any js errors first. then, at the end of your php file, add wp_reset_query() after the loop. wp can get mixed up with multiple queries sometimes, and this helps clear it up.
I encountered a similar problem recently, and it turned out to be related to how the post data was being handled. I recommend ensuring that before your AJAX call runs, you reset the post data. You can do this by adding wp_reset_postdata() right after require_once('../../../wp-load.php'); in your fetch_articles.php. Additionally, consider changing get_permalink() to get_the_permalink() and use get_post_thumbnail_url() instead of the_post_thumbnail_url(), as these functions are typically more reliable in AJAX contexts. Lastly, check your getCategoryColor() function to see if it’s affecting the post loop. You might try temporarily commenting out the badge section to see if that resolves the issue.
Had this exact problem six months ago! The issue was my category query parameter. When you use category_name in WP_Query, it wants the category slug, not the display name. If your category’s called something different in the backend, you’ll only get the first matching post. Switch to category_name with the exact slug, or just use cat with the category ID - way more reliable. You can grab the category ID from Posts > Categories in your WordPress admin. Also check if you’ve got other queries running on the same page. WordPress gets confused when multiple queries are active at once, especially with AJAX stuff.