- This topic has 7 replies, 2 voices, and was last updated 4 years, 4 months ago by
Elvin.
-
AuthorPosts
-
November 29, 2021 at 10:53 pm #2030738
Miguel
I see that by default, when I write something in the “Search Box”, the search process looks through the “Post Titles” and also the “Post Content” in order to show the search results.
Is there a way to get the search results based only on the “Post Titles” ??Thanks in Advance,
MiguelNovember 29, 2021 at 11:45 pm #2030789Elvin
StaffCustomer SupportHi Miguel,
Will this search function have to be toggle-able or is it fine if the search function only searches based on Post titles?
If it has to be toggle-able then it’s best to get a search plugin that can do this for you and replace the default plugin with this PHP snippet.
add_filter( 'generate_navigation_search_output', function() { return sprintf( '<div class="navigation-search">%s</div>', do_shortcode( '[your-search-plugin-shortcode]' ) ); } );If you only want to change the search query to search through post titles only then try the snippet shared in this article – https://www.isitwp.com/limit-search-to-post-titles-only/
November 30, 2021 at 10:11 pm #2032689Miguel
Thank you Elvin for your reply,
I don’t need the search function to be toggle-able. I only need all the searches be based on Post titles. I really don’t want to use any 3rd party plugin, so that the overall website performance keeps unaffected.
Hopefully you can help me with the PHP snippet for this.
November 30, 2021 at 10:20 pm #2032703Elvin
StaffCustomer SupportIf that’s the case then you can try the PHP snippet provided on the article I’ve linked which is this one:
function __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( like_escape( $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', '__search_by_title_only', 500, 2 );Here’s how to add PHP snippets – https://docs.generatepress.com/article/adding-php/
November 30, 2021 at 11:30 pm #2032747Miguel
Hi Kevin, the PHP snippet you provide is working just like I need, filtering only by Post Title. But there is an Warning message caused by the PHP snippet. Below the message:
Warning __search_by_title_only(): Argument #2 ($wp_query) must be passed by reference, value given in wp-includes/class-wp-hook.php on line 303
Thanks in advance for your help
November 30, 2021 at 11:56 pm #2032768Elvin
StaffCustomer SupportOn the line
function __search_by_title_only( $search, &$wp_query )try replacing&$wp_querywith$wp_query.Let us know how it goes.
December 1, 2021 at 12:22 am #2032787Miguel
Thank you very much for your kind help. Now all is working perfectly.
All the Best,
MiguelDecember 1, 2021 at 12:23 am #2032790Elvin
StaffCustomer SupportNo problem. Glad to be of any help. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.