Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using a fucntion to search my post_meta which i store with every post, But the problem is that when i search title, tags, content then the search not work. I think my new function just override the default search of wordpress. Here is my code

function custom_search_query( $query ) {
    $custom_fields = array(
        // put all the meta fields you want to search for here
        "post_state",
        "post_region",
        "post_country"
        );
    $searchterm = $query->query_vars['s'];
    // we have to remove the "s" parameter from the query, because it will prevent the posts from being found
    $query->query_vars['s'] = "";
    if ($searchterm != "") {
        $meta_query = array('relation' => 'OR');
        foreach($custom_fields as $cf) {
            array_push($meta_query, array(
                'key' => $cf,
                'value' => $searchterm,
                'compare' => 'LIKE'
                ));
        }
        $query->set("meta_query", $meta_query);
    }
}
add_filter( "pre_get_posts", "custom_search_query");

So where is i am wrong. Any idea how can i search every thing. My custom post_meta,title,tags,categories,content.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

It is better to modify Wordpress search SQL query instead of adding extra meta query.
try this:

function search_distinct($distinct) {
    $distinct = 'DISTINCT';
    return $distinct;
}

function join_table($join){
    global $wpdb;
    $join .= "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
    return $join;
}

function search_into_post_meta( $search, $wp_query )
{
    if ( is_search() ) {
        global $wpdb;
        if ( empty( $search ) )
            return $search;
        $q = $wp_query->query_vars;
        $n = ! empty( $q['exact'] ) ? '' : '%';
        $search =
        $searchand = '';
         foreach ( (array) $q['search_terms'] as $term ) {
            $term = esc_sql( like_escape( $term ) );
            $search .= "{$searchand} ( ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}') OR ($wpdb->postmeta.meta_key = 'post_state' AND $wpdb->postmeta.meta_value LIKE '{$n}{$term}{$n}') OR ($wpdb->postmeta.meta_key = 'post_region' AND $wpdb->postmeta.meta_value LIKE '{$n}{$term}{$n}') OR ($wpdb->postmeta.meta_key = 'post_country' AND $wpdb->postmeta.meta_value LIKE '{$n}{$term}{$n}'))";
            $searchand = ' AND ';
        }
        if ( ! empty( $search ) ) {
            $search = " AND ({$search}) ";
            if ( ! is_user_logged_in() )
                $search .= " AND ($wpdb->posts.post_password = '') ";
        }
        add_filter('posts_distinct_request', 'search_distinct');
        add_filter('posts_join_request','join_table');
        return $search;
    }
return $search;
}

add_filter( 'posts_search', 'search_into_post_meta', 500, 2 );
share|improve this answer
    
+1 But: You don't need to esc_sql() for like_escape(). Maybe you could explain the exact query variable and its usage a bit. –  kaiser May 22 '13 at 10:37
    
My tags and categories still not working in search, have you any idea @nick –  Adi May 22 '13 at 10:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.