Best-Selling Downloads Dashboard Widget
Lists your five best-selling Easy Digital Downloads products of all time on the dashboard, with their sales count and a link to edit each one.
Opens WPIDE on
Code
add_action( 'wp_dashboard_setup', function () {
if ( ! function_exists( 'edd_get_download_sales_stats' ) || ! current_user_can( 'view_shop_reports' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_edd_top', 'Best-selling downloads', function () {
$ids = get_posts( array(
'post_type' => 'download',
'post_status' => 'publish',
'posts_per_page' => 5,
'fields' => 'ids',
'no_found_rows' => true,
'meta_key' => '_edd_download_sales', // phpcs:ignore WordPress.DB.SlowDBQuery
'orderby' => 'meta_value_num',
'order' => 'DESC',
) );
if ( ! $ids ) {
echo '<p>' . esc_html( 'No sales yet.' ) . '</p>';
return;
}
echo '<ol>';
foreach ( $ids as $id ) {
printf( '<li><a href="%1$s">%2$s</a> – %3$s</li>', esc_url( get_edit_post_link( $id ) ), esc_html( get_the_title( $id ) ), esc_html( sprintf( '%d sales', edd_get_download_sales_stats( $id ) ) ) );
}
echo '</ol>';
} );
} );
Notes
Uses the sales count EDD stores on each download, so the widget is fast even on large stores.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.