1

I noticed that the content of my custom fields is not in my search results. Is there a good remedy for this?

I could just hardcode my content into page templates but I would like the content to be searchable.

Thanks.

1 Answer 1

2

You can use an custom plugin for your custom fields

function fb_custom_search_where($where) {
    // put the custom fields into an array
    $customs = array('custom_field1', 'custom_field2', 'custom_field3');

    foreach( $customs as $custom ) {
    $query .= " OR (";
    $query .= "( m.meta_key = '$custom' )";
    $query .= " AND ( m.meta_value  LIKE '{$n}{$term}{$n}' )";
        $query .= ")";
    }

    $where = " AND ({$query}) AND ($wpdb->posts.post_status = 'publish') ";
    return($where);
}
add_filter( 'posts_where', 'fb_custom_search_where' );

Also you can check the plugin WP Custom Fields Search and you have adhoc an solution.

5
  • very strange code... $term is empty variable so there will be no results. Also i can bet the $wpdb should be global. strange that this answer was chosen, cause it's not working.
    – Tommixoft
    Commented May 19, 2012 at 20:26
  • i think this example was understand from the people, there have ask the question, its only an example.
    – bueltge
    Commented May 23, 2012 at 7:13
  • 4
    This code does not work at all, beware!
    – englebip
    Commented Sep 5, 2012 at 19:28
  • Why, it works on different installs. But it is important, that you change the identifier of the custom fields in the first array.
    – bueltge
    Commented Sep 9, 2012 at 15:19
  • It doesn't work, becuase it is assumed that the people using it have, at least, minimal programming knowledge. Should you modify it correctly, it will modify the SQL query appropriately.
    – Shane
    Commented Dec 3, 2012 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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