(精华)search搜索自定义字段加标题和内容

function custom_search_query( $query ) {
    if ( !is_admin() && $query->is_search && $query->is_main_query() ) {
        // Clean up the search term
        $search_term = sanitize_text_field( $query->query_vars['s'] );

        // Add meta query to search in the custom fields
        $meta_query = array(
            'relation' => 'OR',
            array(
                'key'     => 'partnumber',  // Custom field partnumber
                'value'   => $search_term,
                'compare' => 'LIKE'
            ),
            array(
                'key'     => 'oem_num',     // Custom field oem_num
                'value'   => $search_term,
                'compare' => 'LIKE'
            ),
        );

        // Set the meta query
        $query->set( 'meta_query', $meta_query );

        // Modify the search in title and content as well
        add_filter( 'posts_search', function( $search, $wp_query ) use ( $search_term, $wpdb ) {
            if ( empty( $search ) ) return $search;

            // Build the search query for custom fields and post title/content
            $search .= " OR EXISTS (
                SELECT * FROM $wpdb->postmeta
                WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID
                AND $wpdb->postmeta.meta_key IN ('partnumber', 'oem_num')
                AND $wpdb->postmeta.meta_value LIKE '%$search_term%'
            )";

            return $search;
        }, 10, 2 );
    }
}
add_action( 'pre_get_posts', 'custom_search_query' );

上面的搜索结果是包含media file的, 也就是直接搜索图片的,如果不希望搜索图片的标题, 可以直接在函数里添加

// Exclude attachments (media files) $query->set( ‘post_type’, array( ‘post’, ‘page’, ‘product’ ) );

能搜索全部字段的功能

//同时搜索自定义字段,标题和内容


function custom_search_query( $query ) {
    if ( $query->is_search() && !is_admin() ) {
        // 获取搜索关键词
        $search_term = sanitize_text_field($query->query_vars['s']);

        // 设置 meta_query 以搜索元字段
        $meta_query = array(
            'relation' => 'OR',
            array(
                'key'     => 'oem_num', // 替换为您的元字段键
                'value'   => $search_term,
                'compare' => 'LIKE'
            ),
             array(
                'key'     => 'partnumber', // 替换为您的元字段键
                'value'   => $search_term,
                'compare' => 'LIKE'
            ),
        );

        // 将 meta_query 添加到查询中
        $query->set( 'meta_query', $meta_query );

        // 设置搜索关键词以搜索标题和内容
        $query->set( 's', $search_term ); // 保留搜索关键词
        $query->set( 'post_type', array('product') ); // 搜索所有类型的帖子
        $query->set( 'post_status', 'publish' );
        $query->set( 'post_parent', 0 );


    }
    return $query;
}
add_filter( 'pre_get_posts', 'custom_search_query' );

// 添加自定义搜索条件
function custom_search_where( $where, $query ) {
    global $wpdb;
    
    if ( $query->is_search() && !is_admin() ) {

        $search_term = $query->query_vars['s'];
         $where .= " AND {$wpdb->posts}.post_status = 'publish'";  // Only include published posts
        $where .= " AND {$wpdb->posts}.post_type = 'product'";  // Only search the 'product' post type
        $where .= " OR ({$wpdb->posts}.post_title LIKE '%" . esc_sql( $search_term ) . "%' ";
        $where .= " OR {$wpdb->posts}.post_content LIKE '%" . esc_sql( $search_term ) . "%' ";
        $where .= " OR EXISTS (SELECT * FROM {$wpdb->postmeta} WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID AND {$wpdb->postmeta}.meta_value LIKE '%" . esc_sql( $search_term ) . "%'))";
        
    }

    return $where;
}
add_filter( 'posts_search', 'custom_search_where', 10, 2 );
×
(精华)search搜索自定义字段加标题和内容

(精华)search搜索自定义字段加标题和内容

function custom_sear...