• Hi,

    How to make the search bar only specially work for the title and not the contents? For example, when I type a chinese character in han yu pinyin, only the post with the title containing that character will show but not the post with the content containing that character. Is there any plugin that enables me to only search by title and not by title and content?

Viewing 4 replies - 1 through 4 (of 4 total)
  • AddWeb Solution

    (@addweb-solution-pvt-ltd)

    Hi @tao1wellness,

    1. Install and activate the SearchWP plugin.?This plugin allows you to customize the WordPress search function to search only the post titles.
    2. After installing the plugin, go to the SearchWP settings page and select the “Titles” option under the “Engines” tab.?This will configure the plugin to search only the post titles.

      Hope this helps! ??
    Moderator bcworkz

    (@bcworkz)

    If you are wanting to custom code your own solution, the following is the significant part of the default search’s SQL query for search term “foo”:

    WHERE 1=1
    AND (((wp_posts.post_title LIKE '%foo%')
    OR (wp_posts.post_excerpt LIKE '%foo%')
    OR (wp_posts.post_content LIKE '%foo%')))
    AND...

    You could use the “posts_where” filter to remove the excerpt and content parts of the query. Of course in your case WP will have replaced “foo” with your actual Chinese search term. PHP’s preg_replace() can be used to find and replace string fragments regardless of the search term used through the power of a regular expression.

    Thread Starter taoonewellness

    (@tao1wellness)

    Hi @bcworkz,

    As I am new to wordpress coding, where do I place the line of codes in my wordpress site?

    Moderator bcworkz

    (@bcworkz)

    Custom code can go in your theme’s functions.php file, but if your theme is subject to periodic updates you should create your own child theme. Or code can go in your own custom plugin. Creating a custom plugin is slightly easier than creating a child theme if your bespoke code does not involve custom templates. Plugin Basics

    To be clear, the code I offered earlier is a portion of the SQL that WP generates automatically. It is what needs to be changed by bespoke code you or someone would develop.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Customizing of Search Block’ is closed to new replies.