Skip to content

Instantly share code, notes, and snippets.

@pdewouters
Forked from billerickson/gist:1403244
Created November 29, 2011 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdewouters/1404283 to your computer and use it in GitHub Desktop.
Save pdewouters/1404283 to your computer and use it in GitHub Desktop.
<?php
// Create array for posts, we will stick the attachment post_parent in here
$posts = array();
// Get all images attached to post that have "Include in Rotator" marked as "Yes"
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'menu_order',
'meta_query' => array(
array(
'key' => 'be_rotator_include',
'value' => '1'
)
)
);
$images = new WP_Query( $args );
if( !$images->have_posts() )
return;
while( $images->have_posts() ): $images->the_post(); global $post;
// Add the post_parent to array if not already added
if( !in_array( $post->post_parent, $posts ) )
$posts[] = $post->post_parent;
endwhile;
// Query for all posts in the array
$loop = new WP_Query( array( 'post__in' => $posts ) );
while( $loop->have_posts() ): $loop->the_post(); global $post;
// Do whatever you want with the post here
echo '<h3>' . get_the_title() . '</h3>';
endwhile; wp_reset_query();
wp_reset_query();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment