WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a created a custom post type and have attached some custom fields to it. Now I would like for the search that authors can perform on the custom post list screen (in the admin backend) to also be performed on the meta fileds and not only look in the title and content as usual.

Where can I hook in and what code I have to use?

Example image enter image description here


share|improve this question
up vote 21 down vote accepted

I solved filtering the query by adding the join on the postmeta table and changing the where clause. tips on filtering the WHERE clause (often require regular expression search&replace) are here on codex:

add_filter('posts_join', 'segnalazioni_search_join' );
function segnalazioni_search_join ($join){
    global $pagenow, $wpdb;
    // I want the filter only when performing a search on edit page of Custom Post Type named "segnalazioni"
    if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='segnalazioni' && $_GET['s'] != '') {    
        $join .='LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    return $join;

add_filter( 'posts_where', 'segnalazioni_search_where' );
function segnalazioni_search_where( $where ){
    global $pagenow, $wpdb;
    // I want the filter only when performing a search on edit page of Custom Post Type named "segnalazioni"
    if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='segnalazioni' && $_GET['s'] != '') {
        $where = preg_replace(
       "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    return $where;
share|improve this answer
Wow! Just what I've been looking for. However, I think I may of found a bug, when searching on the post title, I get a match which is then duplicated in the results 5 times!?! imgur.com/eE52gIA – jnthnclrk Aug 22 '13 at 14:58
Here is another grab with the SQL printed: tinypic.com/view.php?pic=124tqb6&s=5 Can't figure out why I get 5 items!?! – jnthnclrk Aug 22 '13 at 20:19
Posted a separate question on fixing the dupe bug: wordpress.stackexchange.com/questions/111185/… – jnthnclrk Aug 24 '13 at 11:47

This will work,

function custom_search_query( $query ) {
    $custom_fields = array(
        // put all the meta fields you want to search for here
    $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");
share|improve this answer
Pleas properly indent your code, and include an explanation as to why and how this will work. – tfrommen Jan 31 '14 at 11:28

Stefano answer is great but it lacks a distinct clause :

function segnalazioni_search_distinct( $where ){
    global $pagenow, $wpdb;

    if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='segnalazioni' && $_GET['s'] != '') {
    return "DISTINCT";

    return $where;
add_filter( 'posts_distinct', 'segnalazioni_search_distinct' );

Add the code above update it and it will work with no duplicates.

share|improve this answer

It is not a search, but some "select" by distinct value.

In file functions-iworks-posts-filter.zip You have a example how to add filter for regular post by some meta_key. I think it is easy to convert.

share|improve this answer
thanks for the help... I'm going to look at your attachmend right now. I'll let you know the results of my investigation ;-) Stefano – Stefano Mar 14 '11 at 8:09
Marcin I think that you are refferring to the filters like "by date" etc... but I need to hook in the "free search" field above. Anyway I just posted my solution, maybe it helps Thank you anyway! – Stefano Mar 18 '11 at 9:03

Your Answer


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.