Created
July 13, 2022 16:51
-
-
Save rniswonger/ddf137dbf6984cfe6c6e1cf1f47498a7 to your computer and use it in GitHub Desktop.
WordPress: Restrict the Customizer menu search to titles
This file contains 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
/** | |
* Restrict Customizer menu search to titles | |
* Based on: https://stackoverflow.com/questions/54155517/how-to-limit-wp-admin-to-search-only-titles | |
* Added Customizer screen detection | |
*/ | |
add_filter( | |
'posts_search', | |
function( $search, $wp_query ) { | |
global $wp_customize; | |
global $wpdb; | |
// leave if we are not in the admin customizer | |
if ( ! is_admin() || ! isset( $wp_customize ) || empty( $search ) ) { | |
return $search; | |
} | |
$q = $wp_query->query_vars; | |
$x = ! empty( $q['exact'] ) ? '' : '%'; | |
$search = ''; | |
$searchand = ''; | |
foreach ( (array) $q['search_terms'] as $term ) { | |
$term = esc_sql( $wpdb->esc_like( $term ) ); | |
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$x}{$term}{$x}')"; | |
$searchand = ' AND '; | |
} | |
if ( ! empty( $search ) ) { | |
$search = " AND ({$search}) "; | |
} | |
return $search; | |
}, | |
10, | |
2 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment