Skip to content

Commit

Permalink
Remove duplicated query
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Apr 26, 2023
1 parent 4c1afee commit 32b84ce
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions wp-content/plugins/wporg-learn/inc/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,28 +269,27 @@ function get_workshop_duration( WP_Post $workshop, $format = 'raw' ) {
function get_available_post_type_locales( $meta_key, $post_type, $label_language = 'english', $post_status ) {
global $wpdb;

$results = isset( $post_status )
? $wpdb->get_col( $wpdb->prepare(
"
SELECT DISTINCT postmeta.meta_value
FROM {$wpdb->postmeta} postmeta
JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id AND posts.post_type = %s AND posts.post_status = %s
WHERE postmeta.meta_key = %s
",
$post_type,
$post_status,
$meta_key
) )
: $wpdb->get_col( $wpdb->prepare(
"
SELECT DISTINCT postmeta.meta_value
FROM {$wpdb->postmeta} postmeta
JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id AND posts.post_type = %s
WHERE postmeta.meta_key = %s
",
$post_type,
$meta_key
) );
$and_post_status = '';
if ( in_array(
$post_status,
array( 'all', 'publish', 'draft', 'trash', 'private', 'needs-vetting', 'unlisted' ),
true
) ) {
$and_post_status = "AND posts.post_status = '$post_status'";
}

// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $and_post_status only includes $post_status from the URL if it matches an allowed string.
$results = $wpdb->get_col( $wpdb->prepare(
"
SELECT DISTINCT postmeta.meta_value
FROM {$wpdb->postmeta} postmeta
JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id AND posts.post_type = %s $and_post_status
WHERE postmeta.meta_key = %s
",
$post_type,
$meta_key
) );
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

if ( empty( $results ) ) {
return array();
Expand Down

0 comments on commit 32b84ce

Please sign in to comment.