Created
March 6, 2025 20:13
-
-
Save russo97/14bd90f16b5464f9aa66d15200217a46 to your computer and use it in GitHub Desktop.
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
| function get_users_who_favorited_post_custom ($post_id, $limit = 4) { | |
| // Define uma chave única para o transient baseada no post_id | |
| $transient_key = 'favorite_users_post_' . $post_id; | |
| // Verifica se o resultado já está em cache | |
| $cached_users = get_transient($transient_key); | |
| if ($cached_users !== false) { | |
| return $cached_users; // Retorna os dados do cache | |
| } | |
| global $wpdb; | |
| // Consulta SQL otimizada com LIMIT | |
| $results = $wpdb->get_results( | |
| $wpdb->prepare( | |
| "SELECT user_id, meta_value | |
| FROM {$wpdb->usermeta} | |
| WHERE meta_key = 'simplefavorites' | |
| AND meta_value LIKE %s | |
| LIMIT %d", | |
| '%i:' . intval($post_id) . ';%', // Filtro para o post_id | |
| $limit | |
| ) | |
| ); | |
| $users = []; | |
| // Processa os resultados | |
| foreach ($results as $row) { | |
| $user_id = $row->user_id; | |
| $serialized_data = $row->meta_value; | |
| // Deserializa os dados | |
| $favorite_posts = maybe_unserialize($serialized_data); | |
| // Verifica se o post_id está no array de favoritos | |
| if (is_array($favorite_posts) && in_array($post_id, $favorite_posts)) { | |
| $users[] = $user_id; | |
| } | |
| // Interrompe o loop se atingir o limite | |
| if (count($users) >= $limit) { | |
| break; | |
| } | |
| } | |
| // Armazena os resultados no cache por 1 hora (ajuste conforme necessário) | |
| set_transient($transient_key, $users, HOUR_IN_SECONDS); | |
| return $users; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment