You don't have to merge the arrays to use them together in a loop. You just need a similar key that you can use as a hash lookup. That's where wp_list_pluck
comes in handy.
// Image Data
$instagrams = array (
array (
'post_id' => 1,
'image' => 'http://placekitten.com/200/300',
), array (
'post_id' => 2,
'image' => 'http://placekitten.com/200/300',
), array (
'post_id' => 3,
'image' => 'http://placekitten.com/200/300',
),
);
// Transform the array where post_id is the key
$hash = wp_list_pluck( $instagrams, 'image', 'post_id' );
// WP_Query arguments
$args = array (
'post_type' => array ( 'post' ),
'post_status' => array ( 'publish' ),
'posts_per_page' => '3',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// the ID from the post
$post_id = get_the_ID();
// becomes the key to the image array
if ( isset( $hash[ $post_id ] ) ) {
$image = $hash[ $post_id ];
echo $image;
}
}
}
wp_reset_postdata();
Other techniques include using an array for the loop with get_posts
, custom sort with usort
or reusing loops with rewind_posts
-- just depends on how clever you want to be.