I have come up with a solution and am sharing in case anybody else would like to use it. Thanks again Alimir, what you provided before lead me in a great direction. I am a novice in PHP and never learned SQL so this journey has been quite the learning experience.
Sort post/media by total like count for the previous month:
First, I set up args for the begin time and the end time I want to query for.
The code below set’s current timezone for dates to be based off of. (I’m in Memphis so mine is America/Chicago…replace with yours) Otherwise it would use the timezone of the server that’s hosting the page.
date_default_timezone_set('America/Chicago');
I set up the start and end times like this because having the way that was suggested above would not actually count the days of the month but base the days off the current month. I also chose this date format because it was how the time_date was displayed in the database table.
$start = date('Y-m-d 00:00:00', strtotime("first day of -1 month")); // First day of the month
$end = date('Y-m-d 23:59:59', strtotime("last day of -1 month")); // 't' gets the last day of the month
The info we needed I believe is beyond the scope of wp_query. (Because I needed to access the _ulike table and columns) So I used $qpdb to do a query directly to the database. Through a lot of trial and error this is what I came up with this:
global $wpdb;
$like_counts = $wpdb->get_results( "
SELECT post_id, COUNT(post_id) as like_number, status
FROM {$wpdb->prefix}ulike
WHERE {$wpdb->prefix}ulike.date_time >= '$start'
AND {$wpdb->prefix}ulike.date_time <= '$end'
AND {$wpdb->prefix}ulike.status = 'like'
GROUP BY {$wpdb->prefix}ulike.post_id
ORDER BY COUNT(post_id)
DESC ");
foreach ($like_counts as $like_count)
That query counts how many times a post occur in the query between the dates above and groups them by the post id.
I then did a MPP_Media_Query (which is similar to WP_Query) so that it would only pull uploaded media instead of media and post, and set id = $like_count->post_id as the only argument for it.
-
This reply was modified 7 years, 6 months ago by Reppiks.