Created
April 25, 2025 09:35
-
-
Save esedic/6927bb6e07c745317daeeae0425efa79 to your computer and use it in GitHub Desktop.
Including custom fields in WordPress search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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