0

I want to retrieve all the image attachments from a posts and render it in a gallery. Some how the number attachments which is being returned is only one even tough the posts contains multiple image attachments. Strangely some of the attachments which are beings returned are not even present in the posts. Below is the function which I am using to get the attchments

function get_attchments($num=1){
  $output = '';
  if (has_post_thumbnail() && $num == 1){
      $output = wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()));
    }else {
      $attachments = get_posts(array(
          'post_type' => 'attachment',
          'posts_per_page' => $num,
          'exclude'     => get_post_thumbnail_id(),
          'post_parent' => get_the_ID(),
      ));

  if ($attachments && $num == 1){
          foreach ($attachments as $attachment){
              $output = wp_get_attachment_url($attachment->ID);
            }
  }else if($attachments && $num > 1){       
          $output = $attachments;
  }
  wp_reset_postdata();
}
 return $output;
}

I am calling the function as below

while(have_posts()){
                    the_post();
                    $def_class='twigs';
                    set_query_var('lighter_def_post_class', 'twigs');
                    get_template_part('template-parts/content', get_post_format() );
                  }

And this is the code for contenet-gallery.php

$attachments = get_attchments(7);          
    foreach( $attachments as $attachment ){
        //I am rendering the images out here
    }

I've been with this issues since long. Thank you

2

1 Answer 1

0

I think you'd have an easier time if you used: $wpdb

global $wpdb;
$table_name = 'posts'; // change this to your post-table name, it may be something different.
$values = $wpdb->get_results( "SELECT * FROM $table_name where post_type = attachment");
foreach ($values as $value){
  print_r($value);
}

If you're seeing unexpected values, then you'll need to figure out if there's anything different about the unexpected values.

By that I mean, are the unexpected values coming from posts which have been deleted? Or attachments that have been moved to the trash but not completely removed? Maybe have a poke around and see if you can find the unexpected attachments and see if you can delete them.

To troubleshoot this further, it would be good to know what's happening in the $wp_query outside of where you're calling this function. using global $wp_query; var_dump($wp_query); exit; can tell you more.

2
  • The unexpected attachments are the images which are being removed from the posts but not permanently . Mar 26, 2018 at 2:40
  • A custom query could help, the 'post_parent__not_in' = array(0) looks promising. wordpress.stackexchange.com/questions/139516/… You'll want to use wp_query, not get_posts (general rule of thumb)
    – admcfajn
    Mar 26, 2018 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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