当前位置: 首页 >wordpress

纯代码免插件实现WordPress搜索自定义字段

wordpress 2017-1-16 阅读量: 1,837 TAG:

自定义字段是WordPress中提供的更强大的功能之一,通过自定义字段,可以扩展WordPress的功能,比如企业网站的产品编号,价格信息,CMS的图片展示等。因而,在用WordPress建设企业网站的时候,通常会用自定义字段增加产品的详细信息。然而,非常不方便的是,自定义字段不可以通过WordPress常规搜索展现,如果需要通过搜索自定义字段查找产品信息,就需要改造WordPress的搜索查询。

实现无插件搜索自定义字段的方法,国外的大神已经给出了方法,这里就做搬运工,分享给大家。所以,使用谷歌搜索就是学**好帮助。

第一步,改造链接查询

由于自字义字段的数据都是存储在数据库”Postmeta”表中,默认情况下,WordPress的搜索功能被设定为搜索”Post”表对应的内容。因而,要实现搜索自定义字段中的数据,首先需要改造链接查询,将”Post”和”Postmeta”连结在一起。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */

function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }
   
    return $join;
}
add_filter('posts_join', 'cf_search_join' );

第二步,修改搜索查询功能

接下来,需要改造的是修改WordPress的搜索查询,让搜索能够展现出自定义字段对应的文章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */

function cf_search_where( $where ) {
    global $pagenow, $wpdb;
   
    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

第三步,过滤重复文章

由于”Postmeta”和”Post”已经加入搜索中,搜索的时候,容易造成同一篇文章出现两次或是多次的现象,所以必须要对重复文章进行过滤。将DISTINCT关键字添加到SQL查询,就可以防止返回重复。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */

function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

最后一步,将以上的代码内容添加到主题模板functions.php,就可以实现通过自定义字段搜索文章。另外,前台样式,需要自行设计修改。

因此,整合所有的步骤,把下面的内容加入主题模板函数文件functions.php中即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
 * Extend WordPress search to include custom fields
 *
 * http://adambalee.com
 */


/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */

function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }
   
    return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */

function cf_search_where( $where ) {
    global $pagenow, $wpdb;
   
    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */

function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
?>

-END-

 

(微信/QQ号:909912499),欢迎分享本文,转载请保留出处!部分内容来自网络,如有侵权请联系删除处理!

相关信息

本站提供代码修改,dedecms,WordPress仿站二次开发 / PHP网站建设以及SEO优化等网络营销推广等服务。

如有需要请加QQ: 909912499