$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>meta query - meta_query with multiple key|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

meta query - meta_query with multiple key

matteradmin8PV0评论

I have a search input then I want to display all the data searched in multiple meta_key. How can I make it?

This is my code

$filter = array(
    'post_type'     => 'request',
    'post_status'   => 'publish',
    'posts_per_page' => -1,
    'meta_query' => array(
        "relation" => "AND",
        array(
            'key' => array($key1, $key2, $key3),
            'value' => $search_value,
            'compare' => 'LIKE',
        )
    )
);

I have a search input then I want to display all the data searched in multiple meta_key. How can I make it?

This is my code

$filter = array(
    'post_type'     => 'request',
    'post_status'   => 'publish',
    'posts_per_page' => -1,
    'meta_query' => array(
        "relation" => "AND",
        array(
            'key' => array($key1, $key2, $key3),
            'value' => $search_value,
            'compare' => 'LIKE',
        )
    )
);
Share Improve this question asked Nov 29, 2017 at 10:13 user132319user132319 11 silver badge1 bronze badge 1
  • try with "compare" => "IN". look here for other possibilities : codex.wordpress/Class_Reference/… – mmm Commented Nov 29, 2017 at 10:46
Add a comment  | 

1 Answer 1

Reset to default 2

key in a meta query needs to be a string, you can't pass multiple keys to a single meta query. You'll need add a query for each key:

$filter = array(
    'post_type'      => 'request',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'meta_query'     => array(
        'relation' => 'OR',
        array(
            'key'     => $key1,
            'value'   => $search_value,
            'compare' => 'LIKE',
        ),
        array(
            'key'     => $key2,
            'value'   => $search_value,
            'compare' => 'LIKE',
        ),
        array(
            'key'     => $key3,
            'value'   => $search_value,
            'compare' => 'LIKE',
        ),
    ),
);

Note that I set 'relation' to 'OR' so that results will be returned for posts that match any of the keys, rather than all. If you need results to have matches for all the keys, change it back to 'AND'.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far