I'm trying to write a loop that excludes posts that don't have attachments in their content areas.
I've got 200 posts in a custom post type called "students". I need to do a query of all students with a gallery or attachment in their content area. NOT their featured image.
Can this be done? This is whats going on with my loop so far. Not working at all, I'm guessing that $gotimages
isn't outputting posts with attachments...
**FINAL EDIT: The loop here works perfectly, the issue that I've found is that you can only assign images once. Which means, if you upload images to a post, then reuse the same images for another post, only the first post image will show. This loop works by association to the post.
Re-upload your images for every post to make this work and have unique images.**
EDIT: Using the loop from s_ha_dum♦, this is getting closer to the thing I'm looking for, though it's only pulling one result in the loop.
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_parent__not_in' => array(0),
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'value' => 'x',
'compare' => 'NOT EXISTS'
)
),
'fields' => 'post_parent'
);
$atts = new WP_Query($args);
$parents = array_unique(wp_list_pluck($atts->posts,'post_parent'));
//var_dump($parents);
// get your students posts
$args = array(
'post_type' => 'student',
'post__in' => $parents
);
$students = new WP_Query($args);
// lets see some results
//var_dump($students->request);
//var_dump($students->posts);
echo '<hr />';
// Looping through the students
// I need to pull a random image from the post' gallery / attachements
// Unsure of how I do this
if ( $students->have_posts() ) {
while ( $students->have_posts() ) {
$students->the_post();
echo get_the_title();
echo '<hr />';
}
} else {
// no posts found
echo 'No students exist';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
query_posts
. Rather useWP_Query
. Rewrite your code and repost your new code.