WARNING: DISARANKAN MENGGUNAKAN THEME GENERATE PRESS
Kasus: default wordpress ketika melakukan pencarian kata di search post dasbord wordpress, maka yang muncul adalah semua postingan yang mengandung kata tersebut, meskipun titlenya tidak mengandung kata tersebut.
Solusi: pastekan kode dibawah ini di function.php theme;
function wpdocs_search_by_title_only( $search, $wp_query ) {
global $wpdb;
if ( empty( $search ) ) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = ! 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 '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter( 'posts_search', 'wpdocs_search_by_title_only', 500, 2 );
Selesai.
Mas Pingi 17.13 17/09/2022
0 Response to "Cara Agar Search Wordpress Hanya Muncul di Title Saja"
Posting Komentar