Search in custom field

Hi,
I have a template for search results of products (WooCommerce). I have a custom field “short_product_synonyms”) for adding synonyms/alternative search terms for products. Is there a way to search in this custom field too? I tried meta query to results but it doesn’t work (but I’m not sure if I tried that correctly).
Thanks
Shortik

Post screenshots of your query… and people can see if it’s set-up right…

You can try a plugin: ACF: Better Search – WordPress plugin | WordPress.org

you can use code: Search using custom fields in wordpress - Stack Overflow

Or post more info for a better answer.

Hi,
thx for reply. So here it is:
I have meta records like this one (alternate words for product search) in wp_postmeta table:
image

On the Search results template I tried something like this in search products query loop, but that doesn’t work:

I won’t be much help, but am replying as it may bump the thread. I’ve found using meta tricky, and often resort to just using the query editor… something like:

[
    'post_type'      => 'product', 
    'posts_per_page' => -1, 
    'meta_query'     => [
        [
            'key'     => 'short_product_synonyms',
            'value'   => get_search_query(), 
            'compare' => 'LIKE',
        ],
    ],
]

Or - add a pre_get_posts to automatically add the field in product searches:

function custom_search_query_with_custom_field($query) {
    // Ensure this only affects the main query and is a search query
   if (!is_admin() && $query->is_main_query() && $query->is_search() && $query->get('post_type') === 'product') {
    
        $search_term = $query->get('s');
        if ($search_term) {
            $meta_query = array(
                array(
                    'key'     => 'short_product_synonyms',
                    'value'   => $search_term,
                    'compare' => 'LIKE'
                )
            );
            $query->set('meta_query', $meta_query);
            $query->set('s', $search_term);
            $query->set('meta_relation', 'OR');
        }
    }
}
add_action('pre_get_posts', 'custom_search_query_with_custom_field');

Maybe that will spark a solution…

1 Like

Have you found a solution? i am facing the same issue

Yes, this tutorial helped me:
Search WordPress by Custom Fields without a Plugin - Adam Balée Designs, LLC (adambalee.com)

My code is:

// Join posts and postmeta tables (http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join)
add_filter('posts_join', 'my_search_join' );
function my_search_join( $join ) {
    global $wpdb;
    if ( is_search() ) {    
        $join .= " LEFT JOIN " . $wpdb->postmeta . " AS my_cf ON " . $wpdb->posts . " .ID = my_cf.post_id AND my_cf.meta_key='my_product_synonyms'";
    }
    return $join;
}

// Modify the search query with posts_where (http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where)
add_filter( 'posts_where', 'my_search_where' );
function my_search_where( $where ) {
    global $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (my_cf.meta_value LIKE $1)", $where );
    }

    return $where;
}

// Prevent duplicates (http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct)
add_filter( 'posts_distinct', 'my_search_distinct' );
function my_search_distinct( $where ) {
    //global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }
    return $where;
}

Thanks a lot Shortik
I managed to get it solved.