0

In want to search by meta data value in post but I didn't succes. For example if I type "sound" I want to found "sounddiary" for example.

Here my code:

function custom_search_query( $query ) {
    if ( $query->is_search ) {  
        $meta_query = $query->get('meta_query');
        $meta_query['relation'] = 'OR';
        $meta_query[] = array( 
            array(
                'key' => 'album-artist',
                'value' =>  $query->query_vars['s'],
                'compare' => 'LIKE'
            )
        );
        $query->set('post_type', 'post');
        $query->set('meta_query', $meta_query);
    };
}
add_filter( 'pre_get_posts', 'custom_search_query');

EDIT:

var_dump($meta_query);

array(1) {
  [0]=>
  array(3) {
    ["key"]=>
    string(21) "album-artist"
    ["value"]=>
    string(5) "diary"
    ["compare"]=>
    string(4) "LIKE"
  }
}
  • var_dump() your query and post the output as an edit to the question. I want to see what the generated meta_query looks like. – s_ha_dum Apr 3 '15 at 17:56
  • There are some plugins that may help you search in meta data. You can use this one: https://wordpress.org/plugins/search-everything/ – mukto90 Apr 3 '15 at 19:40
  • The meta_query is undefined, you are right. – freaky Apr 4 '15 at 8:02

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.