Multiple meta_values use the same meta_key to query posts' custom fields
P粉674876385
2023-08-15 14:16:16
<p>Scenario:
Posts have multiple entries with values for the same key, for example a single post will have multiple meta_key [drink] => meta_value</p>
<pre class="brush:php;toolbar:false;">[drink] => "Banana Juice"
[drink] => "Orange juice"
[drink] => "apple juice"</pre>
<p>Want to find: posts that do not have an orange value in any entry with meta_values with the same meta_key as drink. </p>
<p>Current method: using wp_query: </p>
<pre class="brush:php;toolbar:false;">meta_query => array(array('key' => 'drink','value' => 'orange','compare' => ; 'NOT LIKE'));</pre>
<p>Question:
wp_query will still return posts with meta_value "orange" because it has other [drinks] with values that are not "orange". </p>
In SQL wildcard searches , you need to use the value
%orange%
in NOT LIKE.does not have the
%
wildcard, and NOT LIKE has the same meaning as<>
or NOT EQUAL.