如何配置WordPress搜索仅检索文章内容而非标题?
Hey there! I’ve run into this exact WordPress search issue countless times—the default setup does tend to lean hard into matching post titles over content. Let’s break down three reliable ways to fix this so your search only targets post content:
This is my go-to for quick, code-based control. Just add this snippet to your active theme’s functions.php file (always back up the file first, or use a child theme to avoid losing changes when your theme updates):
function wp_restrict_search_to_content( $search, $wp_query ) { global $wpdb; // Exit early if this isn't the main search query if ( empty( $search ) || !$wp_query->is_search() || !$wp_query->is_main_query() ) { return $search; } // Sanitize the search term to avoid SQL issues $search_term = esc_sql( $wpdb->esc_like( $wp_query->query_vars['s'] ) ); // Replace default search logic with a check only against post content $search = "AND ({$wpdb->posts}.post_content LIKE '%{$search_term}%')"; return $search; } add_filter( 'posts_search', 'wp_restrict_search_to_content', 10, 2 );
What this does: It overrides WordPress’s default search query to only look for matches in the post_content field, ignoring titles, excerpts, or other metadata entirely.
If you’d rather not mess with code, there are solid free plugins that let you customize search behavior:
- WP Extended Search: After installing, head to Settings → WP Extended Search. Under "Search in", uncheck everything except "Post Content", then save your changes. It’s simple and does exactly what you need.
- SearchWP (paid option): For more advanced control (like weighting content more heavily than titles), this plugin lets you fine-tune search priorities with a drag-and-drop interface.
After making changes (whether code or plugin-based), don’t forget to clear your site’s cache (if you use a caching plugin like WP Rocket or W3 Total Cache). Sometimes old search results stick around until the cache is refreshed.
One last thing: If you use custom post types and want them included in the content-only search, make sure to adjust the code snippet (add a check for post_type) or enable those post types in your plugin’s settings.
内容的提问来源于stack exchange,提问作者Donovan

