Skip to content

Instantly share code, notes, and snippets.

@esedic
Created April 25, 2025 09:35
Show Gist options
  • Save esedic/6927bb6e07c745317daeeae0425efa79 to your computer and use it in GitHub Desktop.
Save esedic/6927bb6e07c745317daeeae0425efa79 to your computer and use it in GitHub Desktop.
Including custom fields in WordPress search
<?php
// Source: https://wordpress.stackexchange.com/questions/414404/how-to-include-custom-fields-in-wordpress-search/414408#414408
function include_custom_field_in_search($query) {
if ($query->is_search) {
$query->set('meta_query', array(
array(
'key' => 'release_year',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
}
return $query;
}
add_action('pre_get_posts', 'include_custom_field_in_search');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment