Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major refactor of the Term::format_args() method and conditionally set search fields for term queries in REST API requests. #3869

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions includes/classes/Indexable/Term/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function __construct( $indexable_slug = 'term' ) {

// Filter term query
add_filter( 'terms_pre_query', [ $this, 'maybe_filter_query' ], 10, 2 );

add_filter( 'rest_post_tag_query', [ $this, 'maybe_set_search_fields' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -241,6 +243,27 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) {
return $new_terms;
}

/**
* Conditionally set search fields for term queries in REST API requests.
*
* If in an REST API request that came from WordPress edit screen, do not search for term description.
*
* @param array $prepared_args Array of arguments for get_terms().
* @param WP_REST_Request $request The REST API request.
* @return array
*/
public function maybe_set_search_fields( $prepared_args, $request ) {
$referer = $request->get_header( 'referer' );

if ( empty( $referer ) || ! preg_match( '/post\.php\?post=([0-9]*)&action=edit/', $referer ) ) {
return $prepared_args;
}

$prepared_args['search_fields'] = [ 'name', 'slug' ];

return $prepared_args;
}

/**
* Format the ES hits/results as term objects.
*
Expand Down
Loading
Loading