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!
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.Now we’ve stuffed the important bits into one array. Let’s sort them!
And loop them for final output.
Put this in your functions. It’s a pretty great multidimensional array sorter, referenced above.
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 lastforeach
loop. Let me know if this needs further explanation.If both your Instagram posts and your WordPress Posts are ordered by date descending you should be able to do something like this:
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.