Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm pulling posts from Instagram into the WP basic archive view; but I want things to display in order of date, and not screw up paging (too much). The outcome should look like this:

BLOG POST JUNE 4th

INSTAGRAM IMAGE JUNE 3rd

INSTAGAM IMAGE JUNE 2nd

BLOG POST JUNE 1st

BLOG POST MAY 20

ETC

Is there any easy way to do something like this, other than reading the $wp_query global variable and then pulling only instagrams from that? Something like pulling posts into array and using ksort? Any advice would be appreciated.

UPDATE:

The Instagram feed is integrated via CURL (and an authorized Instagram dev app) and displayed on the page, NOT stored in the WP DB. I realize I could store them but I'm integrating a high-traffic account and don't want to create 100x of posts uselessly (not to mention, why replicate Instagram's existing API?).

SOLUTION:

I used a combination inspired by both the answers below; I ended up getting the "maximum" and "minimum" dates from the posts in the $wp_query object (eg the latest and first) and pulling from between those via the instagram endpoint; I then gathered those into one array, krsort'd it and then relooped and printed out. Thanks all!

share|improve this question
    
The instagram posts are not in the WordPress database at all, in any way? –  s_ha_dum Jun 5 '13 at 15:45
1  
How are you getting this other feed? What does your code look like so far? –  GhostToast Jun 5 '13 at 15:45
    
updated post to answer these questions. –  dama_do_bling Jun 5 '13 at 15:49

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Love this answer and am close to having it setup, but trying to figure out why I'm getting this error: "PHP Warning: array_multisort(): Argument #2 is expected to be an array or a sort flag in /Users/jakelisby/Sites/dewsly.lc/wp-content/themes/dewslyWeb/functions.php(1572) : eval()'d code on line 1" –  Jake Lisby Jul 24 '13 at 16:01
    
Ooops. That's because I was missing a line. Updated. Need flag for direction. can use SORT_ASC or SORT_DESC –  GhostToast Jul 24 '13 at 16:36
    
BOOM! This is perfect, I'm up and running now. When I add in s_ha_dum's answer for the date ranges this will be just delightful. Thanks so much! –  Jake Lisby Jul 24 '13 at 16:41

If both your Instagram posts and your WordPress Posts are ordered by date descending you should be able to do something like this:

if (have_posts()) {
  while (have_posts()) {
    the_post();
    the_content(); // etc.
    while ($instagram_date > $post->post_date && $instagram_date < $posts[$wp_query->current_post +1]) {
      // insert instagram posts
    }
  }
}

Your posts_per_page will be off, as the only thing included in that calculation would be WordPress posts, but pagination should still work.

Obviously that is very rough code-- practically pseudo-code. I don't know how your Instagram posts come in-- array, object-- or what they look like. You will need to work out retrieving the Instagram date and advancing the pointer for the Instagram array/object/whatever-it-is. I assume you'd need an iterator for that array/object or use array_shift perhaps.

Checking the $post->post_date and comparing it to $posts[$wp_query->current_post +1] should be the key to inserting by date though. I think that is the most minimal memory/processing approach.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.