Last active
November 13, 2022 01:00
-
-
Save KaineLabs/5f13046153b218c18402ac2065baedf9 to your computer and use it in GitHub Desktop.
Youzify Albums - Fix albums count
This file contains 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 | |
/** | |
* 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( DISTINCT(media_id) ) 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