Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress: Restrict the Customizer menu search to titles
/**
* 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