retrieve scheduled posts in reverse order with facebook php sdk - php

I have an app that posts a scheduled post to a page 3 hours after the last post.
the problem is that there doesn't seem to be a quick way (that I've found) that can retrieve all scheduled posts in descending order so the post with the latest date is first in the list
is there a fql query or another way that I can bring back all scheduled posts in reverse order so I can grab the scheduled_publish_time from the first array key easily?
the code I'm using is
$posts = $facebook->api($fanpage.'/promotable_posts?limit=500');
$scheduledposts = array();
if($posts){
// find last published post
$time = time();
foreach($posts['data'] as $post){
if($post['is_published'] == false){
$scheduledposts[] = $post;
if($time < $post['scheduled_publish_time']){
$time = $post['scheduled_publish_time'];
}
}
}
$timetopost = $time + 10800;
}
// sort scheduled posts by order of scheduled_publish_time in reverse
usort($scheduledposts, function($a, $b) {
return $b['scheduled_publish_time'] - $a['scheduled_publish_time'];
});
the order of the $posts array isn't always chronological and what if there are more than 500 posts in the schedule? I want to be able to retrieve just the last 5 scheduled posts that have the scheduled_publish_date furthest away

Related

How to get Position of Comment within a Post

So I have a Post which has Comments -- I'm trying to get the Comment # position within that post. For example, a Post has 15 comments, I want to be able to get the numerical position (i.e 1 (first post), 2 (second post), 3 (third post), etc, etc), and put this into a function somehow. That way when I can call $comment->position() and it will show as the 4th, 5th, whatever position in it is in.
I've done some searching around the web and couldn't find a solution. Any help is greatly appreciated! This is what I have so far:
public function position($id,$arr)
{
$total = $this->post->comments->count();
$position = $this->pluck('post_id')->search($this->id) + 1;
return ceil($total / $position);
//$comments_per_page = 25;
//$pos = array_search($id,$arr);
//$pos = $pos+1;
//return ceil($pos/$comments_per_page);
}
You should first get all your comments as collection.
// all comments as collection
$comments = $this->post->comments;
Then you can search through the collection using the search function and inserting an id you want to search for ... or any other param you want.
$id = 2;
$commentIndex = $comments->search(function($comment) use ($id) {
return $comment->id === $id;
});

cyrildewit/eloquent-viewable package views

Im using cyrildewit/eloquent-viewable package to get page views. When I check out the documentation, I don't see a method where I can only get views for a single date. It uses Period and Carbon to get dates in a particular period so the (since and upTo) functions.
Any fix or alternative package to get page views.
I am trying to create a analytics page.
public function pastDateViewsChart(){
$days = [];
$view_count = [];
foreach($this->views as $v){
//get views for the past 28 days
if($v->viewed_at >= Carbon::create("28 days ago")){
if(!in_array(Carbon::parse($v->viewed_at)->format('Y-m-d'),$days,true))
array_push($days, Carbon::parse($v->viewed_at)->format('Y-m-d'));
}
}
//get views for each day
$count = views($this)
->period(Period::upto(Carbon::parse($days[0])))
->count();
array_push($view_count,$count);
return $view_count;
}
You're not limited to since and upto, Period has (as per what I can see there https://github.com/cyrildewit/eloquent-viewable/blob/master/src/Support/Period.php) a create method that can take any date as start and end:
->period(Period::create(
Carbon::parse($days[0])->startOfDay(),
Carbon::parse($days[0])->endOfDay()
))

Insert into array based on items insert date

I am trying to make an array with posts in them, now, i need to insert the posts into the array based on their post date, eg if a post was posted on day 4 it will come after the post that was posted on day 5, so the newest posts gets added and displayed first, is there a good way to do this?
The easiest and not right method at all:
$sortedNews = []; //sorted news
foreach($newsArray as $news) {
$sortedNews[strtotime($news['date'])] = $news; //take date and switch to unix timestamp as key of value
}
krsort($sortedNews); //sort by key (reverse) new to old
The right way it's let your database sort it itself ORDER BY `date` DESC

separate array object according to date

I have a array of events with unixtimestamp and i want to show them according to year. Mean section wise.
2015
Event 1
Event 2
Event 3
2014
Event 1
Event 2
Event 3
What i do:
$yearlyEvents=array();
foreach ($events as $event) {
$eventPost = get_post($event->post_id);
$timestamp=$event->start;
$eventYear=gmdate("Y", $timestamp);
if($index=in_array($eventYear, $yearlyEvents, true)){
print_r($index);
}
else{
$tempObj['name']=$eventYear;
$tempObj['events']=$event;
$yearlyEvents[]=$tempObj;
}
}
But not get the desired results.Anybody help?
You need to get events as sub arrays of year.
Add sub arrays, one for each year append events to it.
This is my preferred logic, please feel free to change it in accordance with your project needs.
Corrected Code:
$yearlyEvents=array();
foreach ($events as $event) {
$eventPost = get_post($event->post_id);
$timestamp=$event->start;
$eventYear=gmdate("Y", $timestamp);
if($index=in_array($eventYear, $yearlyEvents, true)){
print_r($index);
}
else{
$tempObj['name']=$eventYear;
$tempObj['events']=$event;
$yearlyEvents[$eventYear][] = $tempObj; // Check here.
}
}
first of all you need to get result from Database in such a format which is easy to manipulate for your output.
Here you are trying to display events years wise in descending order and events in ascending order. So first of all get result from database like
SELECT * FROM events ORDER by events.start DESC,events.post_id ASC
Now it is in an order which is easy to display. loop through and display result until next year found. check if next year come display year too.

Wordpress Custom loop with two types of posts with different amounts

I have a category names "notes", I need on every page of the loop to show up to 10 notes, and up to 4 regular posts.
They both should be ordered by date.
i.e. I posted 1 note, note A, 1 hour ago and another note, note b, 5 hours ago, I also posted to posts, post A and post B 2 hours ago. in my loop I want to see note A,post A,post B,note B.
I hope it's clear enough.
I'm trying to do this with two custom WP_Queries, but I have hard time with them because of the global $post.
Any help would do!
Thanks
Well, if anyone is interested, this is how I did it:
<?php
function elis_sortbydate ($a, $b) {
$time_a = strtotime($a->post_date);
$time_b = strtotime($b->post_date);
if ($time_a == $time_b) return 0;
return ($time_a > $time_b) ? -1 : 1;
}
$query = array_merge(array('cat' => 67, 'posts_per_page=10'), $wp_query->query);
$notes_query = new WP_Query($query);
$query = array_merge(array('cat' => -67, 'posts_per_page=4'), $wp_query->query);
$posts_query = new WP_Query($query);
if ($notes_query->have_posts() and $posts_query->have_posts()) {
$all_posts = array_merge($notes_query->posts, $posts_query->posts);
usort($all_posts, "elis_sortbydate");
foreach ($all_posts as $post) { ... }
}
the resulting $all_posts array is sorted by date (from high to low)

Categories