对于自定义搜索,PRE_GET_POST()中的元字段搜索过于严格

时间:2015-03-31 作者:atomicadam

我试图找出如何在自定义搜索中放宽与字符串搜索匹配的自定义元字段。

基本上,如果自定义帖子的元字段有“Whistler Bike Park,Canada”,并且我输入了它,我将在搜索中返回帖子。但如果我只输入“Whistler”,那么我什么也得不到。

我认为需要编辑的关键行如下:

$where .= " OR (( $wpdb->postmeta.meta_key = \'$meta_key\' AND $wpdb->postmeta.meta_value = \'$meta_value\' ))";
这是完整的pre\\u get\\u post()挂钩。

add_action(\'pre_get_posts\', \'site_search_query\');
function site_search_query( $query ) 
{
    if (!is_admin() && $query->is_main_query() ) :

        if ( $query->is_search ) :

            // if just a normal search and no in spanish - return 
            // we need to make exception for normal search and is spanish for spanish titles
            // since those are meta values
            if( ( count($_GET) == 1 ) && ( ICL_LANGUAGE_CODE != "es" ) )
                return;

            $s = preg_replace( "/[^a-zA-Z0-9\\s]/", "", $_GET[\'s\'] ); // sanitize and get search string;

            unset( $_GET[\'s\'] );
            $params = array();

            foreach( $_GET as $key => $param ) :

                $params[$key] = preg_replace( "/[^a-z0-9.]+/i", "", $param );

            endforeach;

            $meta_queries = array();

            foreach( $params as $param ) :

                switch( $param ) :

                    case \'org\':
                        $param = \'organization\';
                    break;

                    case \'desc\':
                        $param = \'basic_info_long_description_in_english\';
                    break;

                endswitch;

                $args = array(
                        \'key\' => $param,
                        \'value\' => $s,
                        \'compare\' => \'LIKE\'
                    );

                array_push( $meta_queries, $args );

            endforeach;

            $query->set( \'post_type\', \'project\' );
            $query->set( \'meta_query\', array( $args ) );

            /**
             *  Apparently this is a hack to allow for search by post meta in meta_query for pre_get_post $query->set()
             *  See: http://wpthemetutorial.com/2014/07/17/using-pre_get_posts-meta_query/
             *  this is done as an anonymous function so we can pass it local vars

             *  @param string
             *  @param complex array
             *  @param function
             *  @return string
             */
            $custom_filter = function( $where = \'\' ) use( $meta_queries, $custom_filter ) 
            {

                global $wpdb;

                foreach( $meta_queries as $meta_query ) :

                    $meta_key = $meta_query[\'key\'];
                    $meta_value = $meta_query[\'value\'];

                    // too restrictive
                    $where .= " OR (( $wpdb->postmeta.meta_key = \'$meta_key\' AND $wpdb->postmeta.meta_value = \'$meta_value\' ))";

                    // too loose
                    //$where .= " OR (( $wpdb->postmeta.meta_key = \'$meta_key\' ))";

                endforeach;

                remove_filter( \'posts_where\', $custom_filter );

                return $where;

            };

            add_filter( \'posts_where\', $custom_filter );

        endif; // is_search

    endif; // is not admin && is main query

} // site_search_query
欢迎提出任何建议。

1 个回复
SO网友:atomicadam

似乎我有一个答案——这就是SQL语句的设置方式。

$where .= " OR (( $wpdb->postmeta.meta_key = \'$meta_key\' AND (UPPER($wpdb->postmeta.meta_value) LIKE \'%$meta_value%\') ))";
现在该字段有字符串“Whistler Bike Park,Canada”,但用户只搜索“Whistler”,我得到了我预期返回的帖子。

结束

相关推荐

更改Get_Search_Form的行为

我创建了一个显示搜索框的函数,并将其命名为my_get_search_form(). 那是原件的副本get_search_form() 作用调用函数时,输出如下:Search in http://localhost/wp_ex_3/ <AN EMPTY BOX> <A BROKEN IMAGE LINK>.我怎样才能改变这个丑陋的显示而不显示http://localhost/wp_ex_3/. 我应该说在函数定义中没有\"search in\" 语句,因此另一个函数导致了此行为。上