Integrating post archive wtih another source and preserving date ordering

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

Read More

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!

Related posts

2 comments

  1. 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.

  2. 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.

Comments are closed.