dtd5644 2010-08-17 10:12
浏览 38
已采纳

Wordpress,过滤器来更改搜索词?

I was wondering what the filter was to change the search term in Wordpress?

For example, if someone types in xxx, how could I change that to yyy before it goes into the wordpress search engine?

Cheers.

  • 写回答

2条回答 默认 最新

  • douchujian8124 2010-08-18 05:50
    关注

    Change it when it gets to WordPress, right before WP queries the database:

    $search_replacements = array(
        'find' => 'replace',
        'find2' => 'replace2',
        'var' => 'foo'
    );
    function modify_search_term($request_vars) {
        global $search_replacements;
        if (!empty($request_vars['s']) && !empty($search_replacements[$request_vars['s']])) {
            $request_vars['s'] = $search_replacements[$request_vars['s']];
        }
        return $request_vars;
    }
    add_filter('request', 'modify_search_term');
    

    This will allow you to handle as many conditions as you can think up and add to the $replacements array.

    The 99 in the add_filter is to get it to run late so that you're the last one to make changes to the query (could be important depending upon what other plugins you have installed).

    Your URL will still indicate the original term, but you save a page load. If you have a high traffic site then you don't want to redirect just to get a pretty url.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wordpress如何限制ip访问频率
  • ¥15 自研小游戏,需要后台服务器存储用户数据关卡配置等数据
  • ¥15 请求解答odoo17外发加工某工序的实操方法
  • ¥20 IDEA ssm项目 跳转页面报错500
  • ¥20 系统直接进入应急模式了,请教一下,人要裂开了
  • ¥15 页面初次加载时,JS调用对象可以调用成功,但是一旦跳转到另一个页面,再跳转回来后这个对象就显示undefind了,调用也调用不了了,什么原因?
  • ¥15 来个厉害的程序开发的,安排点活给你做
  • ¥15 rexroth indramotion MTX micro系统轴耦合编程
  • ¥100 springboot2.7.x 整合 sharding 的问题
  • ¥15 如何通过命令行操作统信360安全浏览器?