0

Trying to modify default admin search by including search by custom post type fields,

Below is my code,

function custom_search_query( $query ) {
    $custom_fields = array(
        // put all the meta fields you want to search for here
        "rg_1job_designation",
        "rg_2job_designation"
    );
    $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");

But it's not working.

i have a post title -> John & Designation -> Designer On search by Designer can get 1 result, But on search of John result is empty (This should also fetch one result). Now the default search for title is lost want to use that too.

Did anyone know what's wrong in my code?

  • I realize my answer is not helping, so deleted it. Your problem is something I've came across before. Please refer to this post, I use their code to the rescue. – 1fixdotio Jan 31 '14 at 8:07
  • Use above edited codes now i can able to filter by meta_value. But now default search for title gets lost. Any idea to use both ? – Vin_fugen Jan 31 '14 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.