My goal here is not to save any items to a database, but to just display a live stream.
I am pulling an RSS feed from Huffington Post
http://www.huffingtonpost.com/section/front-page/feed
I have a WordPress array (PHP) of the most recent 50 articles from the Huff.
$rss = fetch_feed($feed_url);
I want my RSS feed to ONLY display X total unique posts per day. To make it simple, I was just going to display the post that is closest to intervals of 24 / X.
For demonstration let's go with 3. The feed would spit out posts published closest to 8, 16 (2 PM), and 24 (midnight) or (0, 8, and 16).
In PHP, how do I sort an object array by a published time variable and then find the closest post to that time? Right now I'm doing a very roundabout way that currently isn't even working.
Here's my current logic:
if(function_exists('fetch_feed')) {
$rss = fetch_feed(get_field('feed_url'));
if(!is_wp_error($rss)) : // error check
$maxitems = $rss->get_item_quantity(50); // number of items at 50
$rss_items = $rss->get_items(0, $maxitems);
endif;
// display feed items ?>
<h1><?php echo $rss->get_title(); ?></h1>
<?php
$coutner = 0;
$daily_max = 3; //how many unique feeds to display per day
$display_interval = floor(24 / $daily_max); //simple way to make even intervals
$posting_time = array(); //to store the times to post
foreach(range(0, $daily_max-1) as $i) {
$posting_time[$i] = $display_interval * $i;
}
$post_interval = 0;
$date = new DateTime();
$today = date("G"); //getting the current day's hour
$time_adjust = $today / $display_interval;
//adjust the posting times order so that its circular
while($today > $posting_time[0]){
$hold = array_pop($posting_time);
echo '<p>hold: ' . $hold;
array_unshift($posting_time,$hold);
}
$accessing = array_pop($posting_time);
?>
<dl>
<?php if($maxitems == 0){ echo '<dt>Feed not available.</dt>';}
else{
foreach ($rss_items as $item) : ?>
<?php
//as soon as the first item is newer than post time, output it & count that time slot as being filled
$rss_item_hour = $item->get_date('G');
if($rss_item_hour > $accessing){ ?>
<dt>
<a href="<?php echo $item->get_permalink(); ?>"
title="<?php echo $item->get_date('j F Y @ G'); ?>">
<?php echo $item->get_title(); ?>
</a>
</dt>
<dd>
<?php echo $item->get_description(); ?>
</dd>
<p>
<?php echo $item->get_date('j F Y | G');
?>
</p>
<?php $coutner = $coutner + 1;
$accessing = array_pop($posting_time);
}
else{echo '<p>else';} ?>
<?php endforeach; ?>
</dl>
<?php }} ?>
The main error right now is that sometimes the circular shifting of while($today > $posting_time[0]){ goes on infinitely. And the loop never seems to go as planned.