Extend search to include custom post meta

This snippet extends WordPress search function to include custom post meta.

Instructions
Add the following code to your functions.php

Comments

8 Replies to “Extend search to include custom post meta”


  1. function custom_search_query( $query ) {
    $custom_fields = array(
    // put all the meta fields you want to search for here
    "_post_title",
    "text_english",
    "text_deutsch"
    );
    $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");
    add_action( "save_post", "add_title_custom_field");

    function add_title_custom_field($postid){
    // since we removed the "s" from the search query, we want to create a custom field for every post_title. I don't use post_content, if you also want to index this, you will have to add this also as meta field.
    update_post_meta($postid, "_post_title", $_POST["post_title"]);
    }

    1. as you remove the ‘s’ from the query. when we search any data, it show there empty like

      Search Results for “”

      So what is the solution for this, so that it not show it empty.

  2. Hi. I have a post type called usr_jardin with a custom field called sim_nuip. I would like to get the search to work for the title of the post and also by custom field . I try this but does not work me.
    function searchfilter($query) {
    $custom_fields = array(
    // put all the meta fields you want to search for here
    “_post_title”,
    “sim_nuip”
    );

    $searchterm = $query->query_vars[‘s’];

    $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);
    };
    if ($query->is_search) {
    $query->set(‘post_type’,array(‘post’,’usr_jardin’));
    } return $query;
    }
    add_filter(‘pre_get_posts’,’searchfilter’);
    add_action( “save_post”, “add_title_custom_field”);

    function add_title_custom_field($postid){
    update_post_meta($postid, “_post_title”, $_POST[“post_title”]);
    }

  3. e.g.
    array_push($meta_query, array(
    ‘keys’ => $prefix. ‘store’,$prefix. ‘add’,$prefix. ‘zip’,$prefix. ‘city’,$prefix . ‘tel’,$prefix . ‘fax’,$prefix . ’email’,$prefix. ‘owner’,
    ‘value’ => $searchterm,
    ‘compare’ => ‘LIKE’
    ));

    if u use ‘keys’ then u insert multiple keys in this function

Leave a Reply

Your email address will not be published. Required fields are marked *