使用PRE_GET_POSTS筛选器在所有元值上搜索多个字符串

时间:2011-12-04 作者:José Pablo Orozco Marín

使用pre\\u get\\u posts筛选器在所有meta\\u值上搜索给定字符串:

$query->set(\'meta_value\', \'first string\');
调试:

AND ( (CAST(wp_postmeta.meta_value AS CHAR) = \'first string\') )
工作正常:)但如何在所有meta\\u值上搜索字符串数组?

例如我需要这样的东西:

$query->set(\'meta_value\', array(\'first string\', \'second string\'));

AND ( (CAST(wp_postmeta.meta_value AS CHAR) = \'first string\') OR (CAST(wp_postmeta.meta_value AS CHAR) = \'second string\') )
可以使用$query->set(\'meta\\u value\'…或其他方式?

提前感谢!

2 个回复
最合适的回答,由SO网友:José Pablo Orozco Marín 整理而成

是的,我知道了:

function custom_search_where( $where = \'\' ) {

    global $wpdb;

    if ( is_search() ) {

        if ( isset($_GET[\'my-terms\']) ) {

            $count = count($_GET[\'my-terms\']);

            if ( $count > 0 ) {

                foreach ( $_GET[\'my-terms\'] as $term ) {

                    // http://codex.wordpress.org/Class_Reference/wpdb

                    $title      = $wpdb->prefix . "posts.post_title";
                    $content    = $wpdb->prefix . "posts.post_content";
                    $meta_value = $wpdb->prefix . "postmeta.meta_value";

                    $term       = trim($term);
                    $my_term    = $wpdb->escape($term);

                    $where .= " AND (($title LIKE \'%$my_term%\') OR ($content LIKE \'%$my_term%\') OR (CAST($meta_value AS CHAR) = \'$my_term\')) ";      

                }

            }

        }

    }

    return $where;

}


add_filter(\'posts_where\', \'custom_search_where\', 999);
以及:

function custom_search_join ($join) {

    global $wpdb;

    if( is_search() ) {

        $existCustomTerms = false;

        if ( isset($_GET[\'my-terms\']) ) {

            $count = count($_GET[\'my-terms\']);

            if ( $count > 0 ) {

                $join .= "INNER JOIN $table ON ($postID = $postmetaID)";

            }

        }

    }

    return $join;

}


add_filter(\'posts_join\', \'custom_search_join\' );

SO网友:Sebastien

我对这段代码做了一些修复,因为它不再工作了。

以下代码将搜索与标题类似的术语OR 内容OR meta\\u值:

function custom_search_where( $where ) {
    if ( is_search() ) {
        global $wpdb;

        // Overwrite the existing WHERE clause.
        $where = \'\';

        // Store all search terms into array.
        $search_terms = explode( \' \', get_search_query() );

        // Tables names.
        $type       = $wpdb->prefix . "posts.post_type";
        $status     = $wpdb->prefix . "posts.post_status";
        $title      = $wpdb->prefix . "posts.post_title";
        $content    = $wpdb->prefix . "posts.post_content";
        $meta_value = $wpdb->prefix . "postmeta.meta_value";

        foreach ( $search_terms as $term ) {
            $term = trim($term);
            $where .= " AND ( ($title LIKE \'%$term%\') OR ($content LIKE \'%$term%\') OR ($meta_value LIKE \'%$term%\') ) ";
        }

        // As WHERE clause is overwritten, you\'ll need to specify the post type, the status and/or anything else you need.
        // Post Types.
        $where .= " AND ($type IN (\'post\', \'page\', ... )) ";
        // Post status.
        $where .= " AND ($status = \'publish\') ";
    }
    return $where;
}
add_filter(\'posts_where\', \'custom_search_where\', 999);
以及

function custom_search_join ($join) {
    if( is_search() ) {
        global $wpdb;
        $join .= " INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)";
    }
    return $join;
}
add_filter(\'posts_join\', \'custom_search_join\' );

结束

相关推荐