Wordpress搜索结果postmeta和term

时间:2018-03-09 21:41:36

标签: mysql wordpress join

这有什么问题?

我正在尝试搜索wp_postmeta和wp_terms,但我得到了一个或另一个,从来没有。我已经尝试在WHERE中将OR更改为AND,但没有任何反应,也没有打印结果。

谢谢!

$join .= "

    LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id 
    LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id 
    INNER JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id 
    INNER JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id 

";

这......

$where .= " 

    OR ( $wpdb->postmeta.meta_value LIKE '%".get_search_query()."%' AND $wpdb->posts.post_status = 'publish' ) 
    OR ( $wpdb->terms.name LIKE '%".get_search_query()."%' AND $wpdb->posts.post_status = 'publish' ) 

";

更新1

在这里阅读https://support.advancedcustomfields.com/forums/topic/mistake-in-documentation-query-posts-by-custom-fields/之后 - 我已经改变了“加入”的两个“内部联接”,它运行良好。我不知道它为什么会起作用。但不幸的是,结果仍然很慢。

你能告诉我这是否是我正在寻找的最佳方法吗?

add_filter( 'posts_join', 'custom_search_join' );
add_filter( 'posts_where', 'custom_search_where' );
add_filter( 'posts_distinct', 'custom_search_distinct' );


function custom_search_join( $join ) {

global $wpdb;

if( !is_admin() ) {

    if( is_search() ) {

        $join .= "

        LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id 
        LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id 
        LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id 
        LEFT JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id 

        ";

        print_r( $join ) ;

    }

}

return $join;

}

function custom_search_where( $where ) {

global $wpdb;

if( !is_admin() ) {

    if( is_search() ) {

        $where .= " 

        OR ( $wpdb->postmeta.meta_value LIKE '%".get_search_query()."%' ) 
        OR ( $wpdb->terms.name LIKE '%".get_search_query()."%' ) 
        AND $wpdb->posts.post_status = 'publish'

        ";

        print_r( $where ) ;

    }

}

return $where;

}

function custom_search_distinct( $where ) {

global $wpdb;

if( !is_admin() ) {

    if( is_search() ) {

        return "DISTINCT";

    }

}

return $where;

}

更新2

经过一些研究和研究。

  1. 定义将要搜索的ACF字段列表
  2. 定义要搜索的分类法
  3. 到目前为止跑得快。 如果有人知道改进此代码的方法,我真的很感激。

    add_filter( 'posts_search', 'busca_acf', 500, 2 );
    
    function lista_campos_acf() {
    
        $lista_campos_acf = [ 'acf-field-example' ];
        return $lista_campos_acf;
    
    }
    
    function lista_taxonomias() {
    
        $lista_taxonomias = [ 'post_tag', 'category', 'example_taxonomy' ];
        return $lista_taxonomias;
    
    }
    
    function busca_acf( $where, $wp_query ) {
    
    if( !is_admin() ) {
    
        global $wpdb;
    
        $termos = remove_accents( $wp_query->query_vars[ 's' ] );
    
        $exploded = array_map( 'strtolower', explode( ' ', $termos ) );
    
        $lista_campos_acf = lista_campos_acf();
    
        $lista_taxonomias = lista_taxonomias();
    
        $where = '';
    
        foreach( $exploded as $tag ) {
    
            $where .= " 
    
                AND (
    
                    ( wp_posts.post_title LIKE '%$tag%' ) 
    
                    OR ( wp_posts.post_content LIKE '%$tag%' ) 
    
                    OR EXISTS (
    
                        SELECT * 
                        FROM wp_postmeta
                        WHERE post_id = wp_posts.ID
                        AND ( ";
    
                            foreach( $lista_campos_acf as $campo_acf ) {
    
                                if( $campo_acf == $lista_campos_acf[0] ) {
    
                                    $where .= " ( meta_key LIKE '%" . $campo_acf . "%' AND meta_value LIKE '%$tag%' ) ";
    
                                }
    
                                else {
    
                                    $where .= " OR ( meta_key LIKE '%" . $campo_acf . "%' AND meta_value LIKE '%$tag%' ) ";
    
                                }
    
                            }
    
                        $where .= ")
    
                    )
    
                    OR EXISTS (
    
                        SELECT * 
                        FROM wp_terms 
                        INNER JOIN wp_term_taxonomy ON wp_term_taxonomy.term_id = wp_terms.term_id 
                        INNER JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id 
                        WHERE ( ";
    
                            foreach ( $lista_taxonomias as $tax ) {
    
                                if( $tax == $lista_taxonomias[0] ) {
    
                                    $where .= "taxonomy = '" . $tax . "'";
    
                                }
    
                                else {
    
                                    $where .= "OR taxonomy = '" . $tax . "'";
    
                                }
    
                            }
    
                        $where .= ")
                        AND object_id = wp_posts.ID
                        AND wp_terms.name LIKE '%$tag%'
    
                    )
    
                )
    
            ";
    
        }
    
        return $where;
    
    }
    
    }
    

0 个答案:

没有答案