Here's my problem: I'm trying to display a gallery of images attached to a post via ajax. The post is also loaded dynamically via ajax from the homepage. The query to retrieve the images however returns an empty array. The strange thing is, the same query actually works if the gallery is loaded via ajax, while the post is loaded "traditionally". To be more clear:
1st CASE:
- I'm in INDEX.php
- I load the post dynamically via ajax in INDEX.php
- I launch the gallery dynamically via ajax from the post I've just loaded
- The gallery DOES NOT work
2nd CASE:
- I'm in SINGLE.php (meaning the post is loaded traditionally, not via ajax)
- I launch the gallery dynamically via ajax
- The gallery WORKS
Here's the query I'm using to retrieve the attaches images:
// The post id I'm fetching from the AJAX call that loads the gallery
$post_id = intval( $_POST['post_id'] );
$args = array(
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post_id,
'post_status' => 'inherit',
'post_type' => 'attachment'
);
$images = get_posts( $args );
if ( $images ) :
// Loop
else :
// No posts
endif;
And of course, unfortunately I get 'No posts' in the first case, while everything's fine in the second case... What's the difference?
Many thanks for your help!