Created
January 23, 2015 18:15
-
-
Save thefrosty/ba90b2ba4d0c5e823991 to your computer and use it in GitHub Desktop.
Get popular downloads for recent XX days. Easy Digital Downloads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ap_get_edd_logs( $args = array() ) { | |
global $edd_logs; | |
// Prevent the queries from getting cached. Without this there are occasional memory issues for some installs | |
wp_suspend_cache_addition( true ); | |
$logs_data = array(); | |
$download = null; | |
$defaults = array( | |
'posts_per_page' => 40, | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
$log_query = array( | |
'post_parent' => $download, | |
'log_type' => 'sale', | |
'posts_per_page' => $args['posts_per_page'], | |
); | |
$logs = $edd_logs->get_connected_logs( $log_query ); | |
if ( $logs ) { | |
foreach ( $logs as $log ) { | |
$payment_id = get_post_meta( $log->ID, '_edd_log_payment_id', true ); | |
// Make sure this payment hasn't been deleted | |
if ( get_post( $payment_id ) ) : | |
$logs_data[] = array( | |
'download_id' => $log->post_parent, | |
); | |
endif; | |
} | |
} | |
return $logs_data; | |
} | |
function ap_edd_get_recent_download_ids( $posts = 40, $recent = 6 ) { | |
// 40 seems like a good number as an estimate of orders in the recent month. | |
$downloads = ap_get_edd_logs( array( 'posts_per_page' => $posts ) ); | |
$ids = $recent_download_ids = array(); | |
if ( $downloads ) { | |
foreach( $downloads as $key => $value ) { | |
$recent_download_ids[] = $value['download_id']; | |
} | |
// Order by unique values and count them. | |
$values = array_count_values( $recent_download_ids ); | |
// Sort from order 'large' to 'small', so more 'orders' is higher in the array. | |
arsort( $values, SORT_NUMERIC ); | |
/** | |
* query_args | |
* | |
* @uses array_slice — Extract a slice of the array ( (int) 0 - [starting key], (int) $recent [max key number] ) | |
* @uses array_keys — Return all the keys or a subset of the keys of an array | |
*/ | |
$ids = array_slice( array_keys( $values ), 0, $recent ); | |
} | |
return $ids; | |
} | |
$key = 'edd_get_popular_downloads'; | |
$query = wp_cache_get( $key ); | |
if ( false === $query ) { | |
$post__in = edd_get_recent_download_ids( 30, 3 ); | |
$query_args = array( | |
'post_type' => 'download', | |
'post_status' => 'publish', | |
'posts_per_page' => 6, | |
'post__in' => $post__in | |
); | |
$query = new WP_Query( $query_args ); | |
wp_cache_set( $key, $query ); | |
} | |
while ( $query->have_posts() ) : $query->the_post(); | |
// Loop | |
endwhile; | |
wp_reset_query(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm going to follow up and say my use is a special use case. I've moved outside of downloads as my post type. I ended up figuring it out after a quick break. Since I've got multiple post types that use downloads for the front end I was using CPT-onomies to tie them together. This is the $args I had to update to: