Skip to content

Commit bc33e6e

Browse files
Ensure we return the current context row first
1 parent d8304a1 commit bc33e6e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

php/relate/class-relationship.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ public function get_data() {
8989
// Create the context query placeholders by filling an array with the correct number of %s placeholders and then imploding it into a string.
9090
$context_query = implode( ', ', array_fill( 0, count( $contexts ), '%s' ) );
9191

92+
// Prepare arguments for the SQL query.
93+
$query_args = array_merge(
94+
array( $this->post_id ),
95+
$contexts,
96+
array( $this->context )
97+
);
98+
9299
// phpcs:ignore WordPress.DB
93100
$sql = $wpdb->prepare(
94-
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query})", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
95-
$this->post_id,
96-
...$contexts
101+
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query}) ORDER BY FIELD(`media_context`, %s) DESC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
102+
$query_args
97103
);
98104
$data = $wpdb->get_row( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB
99105

0 commit comments

Comments
 (0)