programing

WooCommerce 제품 검색을 사용하여 특정 post_type 양식 post_type 열을 검색하는 중 오류 발생

jooyons 2023. 7. 4. 21:53
반응형

WooCommerce 제품 검색을 사용하여 특정 post_type 양식 post_type 열을 검색하는 중 오류 발생

는 이 코드를 사용하여 워드프레스/우커머스 웹사이트에서 제품을 검색하고 있습니다.
제 요구 사항은 URL이 "http://localhost/wp/?s=D34&post_type=product"와 같은 상태가 되는 것입니다.s=D34검색 문자열입니다.

사용자가 문자열을 검색할 시기입니다.모든 기본 필드 + 제품에서 데이터가 검색됩니다.custom filed아래 코드는 http://localhost/wp/?s=D34에서 작동하지만 다음과 같은 경우&post_type=producturl과 연결되면 다음과 같이 표시됩니다.

코드는 아래와 같습니다.

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 );
        $where .= " AND ($wpdb->posts.post_type = 'product') ";
    }

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

이는 별개의 값을 방지하기 위한 것입니다.

  function cf_search_distinct( $where ) {
        global $wpdb;
if ( is_search() ) {
    return "DISTINCT"; //to prevent duplicates
}

return $where;

}
add_filter( 'posts_distinct', 'cf_search_distinct' );

그렇다면 어떤 수정이 필요합니까?

URL http://localhost/wp/?= price&post_type= 제품별 주문은 정상입니다.

그런데 http://localhost/wp/?s=D34&post_type=제품에 무슨 문제가 있습니까?

이것을 먹어보세요.

function cf_search_where( $where ) {
    global $pagenow, $wpdb;

    // a little debugging will help you..
    //print_r ($where);
    //die();

    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 );
        $where .= " AND ($wpdb->posts.post_type = 'product') ";
    }

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

업데이트된 질문을 기반으로 합니다.

당신이 사용했더라면print_r ($where);가치가 무엇을 하는지 확인하기 위해$where다음과 같은 것을 보게 될 것입니다.

http://localhost/wp/?s=D34 사용

AND (((wp1_posts.post_title LIKE '%D34%') OR (wp1_postmeta.meta_value LIKE '%D34%') OR (wp1_posts.post_content LIKE '%D34%'))) 
AND (wp1_posts.post_password = '') 
AND wp1_posts.post_type IN ('post', 'page', 'attachment', 'product') 
AND (wp1_posts.post_status = 'publish')

http://localhost/wp/?s=D34&post_type= 제품 포함

AND (((wp1_posts.post_title LIKE '%D34%') OR (wp1_postmeta.meta_value LIKE '%D34%') OR (wp1_posts.post_content LIKE '%D34%'))) 
AND (wp1_posts.post_password = '') 
AND ( ( wp1_postmeta.meta_key = '_visibility' AND CAST(wp1_postmeta.meta_value AS CHAR) IN ('visible','search') ) ) 
AND wp1_posts.post_type = 'product' 
AND (wp1_posts.post_status = 'publish')

유의하다, 주목하다wp1_posts.post_type힌트를...당신 자신에게 유연하고 디버그를 시도하세요. 위의 결과들은 다음과 같습니다.$where .= " AND ($wpdb->posts.post_type = 'product') ";그래도.

언급URL : https://stackoverflow.com/questions/34349462/error-in-searching-specific-post-type-form-post-type-column-using-woocommerce-pr

반응형