Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_posts() always empty for post_type attachment

Tags:

wordpress

I am struggling with the following while working on a Wordpress theme:

as the title suggests, the array returned by get_posts() is frequently empty despite posts definitely holding images.

I am using the following to retrieve the attachments-array:

$attachments = get_posts( array(
    'post_type' => 'attachment',
    'posts_per_page' => -1,
    'post_parent' => $post_id
) );

Now, the $post_id works perfectly fine...if I echo it right before the above snippet it shows without fail. I can't make out where the error is.

For the sake of completeness, heres the whole loop, which works perfectly fine in every regard except the attachment retrieval:

           <?php if (have_posts()) : ?>
            <?php while (have_posts()) : ?>
                <?php the_post(); ?>
                <div class="post" id="post-<?php the_ID(); ?>">
                    <div class="post-border">
                        <div class="post-date"><?php edit_post_link('Edit Post', '', ''); ?></div>
                        <?php if($pagename == 'news'): ?>
                            <div class="post-date">Posted: <?php the_time('F j, Y') ?></div>
                        <?php endif; ?>
                        <h5 class="posttitle"><?php the_title(); ?></h5>
                        <div class="post-entry">
                            <?php the_content(); ?>
                            <?php
                                $post_id = $post -> ID;
                                echo $post_id;
                                $attachments = get_posts( array(
                                    'post_type' => 'attachment',
                                    'posts_per_page' => 20,
                                    'post_parent' => $post_id
                                ) );
                                print_r($attachments);
                            ?>
                        <?php echo "<div class='clearboth'></div>"; ?>
                        </div> <!-- end of .entry -->
                    </div> <!-- end of .post-border -->
                </div> <!-- end of .post -->
            <?php endwhile; ?>
        <?php endif; ?>

If anyone has any suggestions I'd be very grateful!

Best, J

like image 753
JoW Avatar asked Oct 17 '25 19:10

JoW


2 Answers

Attachments inherit post_status from the parent post, so add the following to the args:

'post_status' => 'inherit',
like image 147
diggy Avatar answered Oct 20 '25 17:10

diggy


I found out in Wordpress 4.1 that when you insert an image from the images previously uploaded to the media library, it is not treated as attachment. So, in order to create image attachments for the post, the images should be first uploaded as files to media library ("add media-"upload files) and only then selected and inserted into the post.

like image 44
user3695711 Avatar answered Oct 20 '25 15:10

user3695711



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!