Without knowing more about what your Instagram data feed object looks like, I would do something like this, assuming you already have a $wp_query
object with real posts in it.
// we're going to merge both objects into one array so we can sort it
$merge_array = array();
// run through actual posts, assuming you already have a wp_query object built
while($loop->have_posts()): $loop->the_post();
$merge_array[] = array(
// store original post ID for later reference
'post_id' => $post->ID,
'date' => date('U', strtotime($post->post_date)),
);
endwhile;
// now loop through instagram stuff and fill up merge array with it as well
foreach($mysterious_instgram_ojbect as $instagram_image){
$merge_array[] = array(
'post_id' => false, // set this false to know it is not WP post object
'date' => date('U', $instagram_image->date), // guessing here
// store more info here such as image URL and/or title
);
}
Now we’ve stuffed the important bits into one array. Let’s sort them!
// sort them by date, function defined below
$merge_array_sorted = array_msort($merge_array, array('date' => SORT_DESC));
And loop them for final output.
foreach($merge_array_sorted as $feed_object){
echo date('F js', $feed_object['date']);
if($feed_object['post_id']){
// is post, treat as wordpress post!
echo get_the_title($feed_object['post_id']);
echo get_permalink($feed_object['post_id']);
} else {
// is instagram, treat accordingly
}
}
Put this in your functions. It’s a pretty great multidimensional array sorter, referenced above.
// world famous multidimensional array sort function
function array_msort($array, $cols) {
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array();
foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
}
$eval="array_multisort(";
foreach ($cols as $col => $order) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr($eval,0,-1).');';
eval($eval);
$ret = array();
foreach ($colarr as $col => $arr) {
foreach ($arr as $k => $v) {
$k = substr($k,1);
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
This is a loose example. The objects should now be sorted by a Unix time stamp, which you can convert back to a more usable date. You can easily reference WP functions like the_content()
and dress it all up in your last foreach
loop. Let me know if this needs further explanation.