限定 WordPress 只搜索文章的标题

前端之家收集整理的这篇文章主要介绍了限定 WordPress 只搜索文章的标题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

站长朋友们在使用wordpress建站的过程中会发现,wordpress 默认的搜索功能还是挺弱的,搜索出来的结果不符合人意,如果您想让它检索出来的结果更加相关,可以限定只搜索文章标题,下面我们就来介绍一下实现的方法

切换到主题目录,打开 functions.PHP 文件,加入以下代码即可:

Crayon-5c891cc516804119172092" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc516804119172092-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-2">2
Crayon-num" data-line="Crayon-5c891cc516804119172092-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-4">4
Crayon-num" data-line="Crayon-5c891cc516804119172092-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-6">6
Crayon-num" data-line="Crayon-5c891cc516804119172092-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-8">8
Crayon-num" data-line="Crayon-5c891cc516804119172092-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-10">10
Crayon-num" data-line="Crayon-5c891cc516804119172092-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-12">12
Crayon-num" data-line="Crayon-5c891cc516804119172092-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-14">14
Crayon-num" data-line="Crayon-5c891cc516804119172092-15">15
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-16">16
Crayon-num" data-line="Crayon-5c891cc516804119172092-17">17
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-18">18
Crayon-num" data-line="Crayon-5c891cc516804119172092-19">19
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-20">20
Crayon-num" data-line="Crayon-5c891cc516804119172092-21">21
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-22">22
Crayon-num" data-line="Crayon-5c891cc516804119172092-23">23
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-24">24
Crayon-num" data-line="Crayon-5c891cc516804119172092-25">25
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc516804119172092-26">26
Crayon-num" data-line="Crayon-5c891cc516804119172092-27">27
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc516804119172092-1">Crayon-c">/**
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-2">Crayon-c"> * Search sql filter for matching against post title only.
Crayon-line" id="Crayon-5c891cc516804119172092-3">Crayon-c"> * @param string$search
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-4">Crayon-c"> * @param WP_Query$wp_query
Crayon-line" id="Crayon-5c891cc516804119172092-5">Crayon-c">*/
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-6">Crayon-t">functionCrayon-h"> Crayon-e">wp_search_by_titleCrayon-sy">(Crayon-h"> Crayon-v">$searchCrayon-sy">,Crayon-h"> Crayon-v">$wp_queryCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cc516804119172092-7">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-o">!Crayon-h"> Crayon-k ">emptyCrayon-sy">(Crayon-h"> Crayon-v">$searchCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-o">&&Crayon-h"> Crayon-o">!Crayon-h"> Crayon-k ">emptyCrayon-sy">(Crayon-h"> Crayon-v">$wp_queryCrayon-i">query_varsCrayon-sy">[Crayon-s">'search_terms'Crayon-sy">]Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-8">Crayon-h">Crayon-m">globalCrayon-h"> Crayon-v">$wpdbCrayon-sy">;
Crayon-line" id="Crayon-5c891cc516804119172092-9">
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-10">Crayon-h">Crayon-v">$qCrayon-h"> Crayon-o">=Crayon-h"> Crayon-v">$wp_queryCrayon-i">query_varsCrayon-sy">;
Crayon-line" id="Crayon-5c891cc516804119172092-11">Crayon-h">Crayon-v">$nCrayon-h"> Crayon-o">=Crayon-h"> Crayon-o">!Crayon-h"> Crayon-k ">emptyCrayon-sy">(Crayon-h"> Crayon-v">$qCrayon-sy">[Crayon-s">'exact'Crayon-sy">]Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">?Crayon-h"> Crayon-s">''Crayon-h"> Crayon-o">:Crayon-h"> Crayon-s">'%'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-12">
Crayon-line" id="Crayon-5c891cc516804119172092-13">Crayon-h">Crayon-v">$searchCrayon-h"> Crayon-o">=Crayon-h"> Crayon-t">arrayCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-14">
Crayon-line" id="Crayon-5c891cc516804119172092-15">Crayon-h">Crayon-st">foreachCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-sy">(Crayon-h"> Crayon-t">arrayCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-v">$qCrayon-sy">[Crayon-s">'search_terms'Crayon-sy">]Crayon-h"> Crayon-st">asCrayon-h"> Crayon-v">$termCrayon-h"> Crayon-sy">)
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-16">Crayon-h">Crayon-v">$searchCrayon-sy">[Crayon-sy">]Crayon-h"> Crayon-o">=Crayon-h"> Crayon-v">$wpdbCrayon-e">prepareCrayon-sy">(Crayon-h"> posts.post_title LIKE %s"Crayon-sy">,Crayon-h"> Crayon-v">$nCrayon-h"> Crayon-sy">.Crayon-h"> Crayon-v">$wpdbCrayon-e">esc_likeCrayon-sy">(Crayon-h"> Crayon-v">$termCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-v">$nCrayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891cc516804119172092-17">
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-18">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-o">!Crayon-h"> Crayon-e">is_user_logged_inCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">)
Crayon-line" id="Crayon-5c891cc516804119172092-19">Crayon-h">Crayon-v">$searchCrayon-sy">[Crayon-sy">]Crayon-h"> Crayon-o">=Crayon-h"> posts.post_password = ''"Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-20">
Crayon-line" id="Crayon-5c891cc516804119172092-21">Crayon-h">Crayon-v">$searchCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">' AND 'Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-e">implodeCrayon-sy">(Crayon-h"> Crayon-s">' AND 'Crayon-sy">,Crayon-h"> Crayon-v">$searchCrayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-22">Crayon-h">Crayon-sy">}
Crayon-line" id="Crayon-5c891cc516804119172092-23">
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-24">Crayon-h">Crayon-k ">returnCrayon-h"> Crayon-v">$searchCrayon-sy">;
Crayon-line" id="Crayon-5c891cc516804119172092-25">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891cc516804119172092-26">
Crayon-line" id="Crayon-5c891cc516804119172092-27">Crayon-e">add_filterCrayon-sy">(Crayon-h"> Crayon-s">'posts_search'Crayon-sy">,Crayon-h"> Crayon-s">'wp_search_by_title'Crayon-sy">,Crayon-h"> Crayon-cn">10Crayon-sy">,Crayon-h"> Crayon-cn">2Crayon-h"> Crayon-sy">)Crayon-sy">;

有需要的朋友可以使用上面的代码测试一下。

原文链接:https://www.f2er.com/wordpress/238376.html

猜你在找的wordpress相关文章