From 6fa4084177723f78a2a47ce42b8eed3df95060f3 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Sun, 27 Oct 2024 23:12:31 +0100 Subject: [PATCH] Optimize token track meta fields --- includes/functions/_setup-meta-fields.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/includes/functions/_setup-meta-fields.php b/includes/functions/_setup-meta-fields.php index 2af2f597..f2e1a33f 100644 --- a/includes/functions/_setup-meta-fields.php +++ b/includes/functions/_setup-meta-fields.php @@ -698,10 +698,27 @@ function fictioneer_get_metabox_tokens( $post, $meta_key, $options, $args = [] ) // Setup $array = get_post_meta( $post->ID, $meta_key, true ); $array = is_array( $array ) ? $array : []; + $array = array_map( 'absint', $array ); // Array should already be safe anyway + $titles = []; $list = esc_attr( implode( ', ', $array ) ); $label = strval( $args['label'] ?? '' ); $description = strval( $args['description'] ?? '' ); + // Query post titles if required + if ( ! ( $args['names'] ?? 0 ) ) { + global $wpdb; + + $placeholders = implode( ',', array_fill( 0, count( $array ), '%d' ) ); + + $sql = + "SELECT p.ID, p.post_title + FROM {$wpdb->posts} p + WHERE p.ID IN ($placeholders)"; + + $results = $wpdb->get_results( $wpdb->prepare( $sql, ...$array ), OBJECT_K ); + $titles = wp_list_pluck( $results, 'post_title', 'ID' ); + } + ob_start(); // Start HTML ---> ?> @@ -725,7 +742,7 @@ function fictioneer_get_metabox_tokens( $post, $meta_key, $options, $args = [] ) if ( $args['names'] ?? 0 ) { $name = $args['names'][ $token ] ?? $token; } else { - $name = get_the_title( $token ) ?: $token; + $name = ( $titles[ $token ] ?? 0 ) ? $titles[ $token ] : $token; } echo "{$name}";