Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Youzify Albums - Fix albums count
<?php
/**
* Youzify Albums - Fix albums count
**/
add_action( 'init', 'yzc_fix_youzify_albums_count', 10);
function yzc_fix_youzify_albums_count( ) {
if ( get_option( 'yzc_albums_fix_albums_count' ) ) {
return;
}
update_option( 'yzc_albums_fix_albums_count', 1, false );
global $wpdb;
$duplicated_ids = $wpdb->get_results( 'SELECT *, COUNT(*) FROM ' . $wpdb->prefix . 'youzify_album_items group BY media_id, album_id, user_id, item_id, component, privacy HAVING COUNT(*) > 1' );
if ( ! empty( $duplicated_ids ) ) {
foreach( $duplicated_ids as $field ) {
$wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'youzify_album_items WHERE id = ' . $field->id );
}
}
// Albums Query.
$albums_query = array(
'posts_per_page' => -1,
'post_status' => 'publish',
'post_type' => 'youzify_albums',
'fields' => 'ids'
);
$albums = get_posts( $albums_query );
if ( ! empty( $albums ) ) {
foreach( $albums as $album_id ) {
$count = $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'youzify_album_items WHERE album_id = ' . $album_id );
yzalbums_update_meta( $album_id, 'items_count', $count );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment