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:

  1. I'm in INDEX.php
  2. I load the post dynamically via ajax in INDEX.php
  3. I launch the gallery dynamically via ajax from the post I've just loaded
  4. The gallery DOES NOT work

2nd CASE:

  1. I'm in SINGLE.php (meaning the post is loaded traditionally, not via ajax)
  2. I launch the gallery dynamically via ajax
  3. 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

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!

  • Maybe the post-id is not POSTed correctly? Did you try just displaying the post_id the script got? Sep 4, 2017 at 9:54

1 Answer 1


Stupid me! There was an error in my AJAX call so I was passing the wrong post ID in the second case. So, nevermind guys.

(Is there a to delete a question? Can't seem to find one).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.