Hopefully i understood correctly what you want to achieve here.
If you want to exclude all posts that contain get_current_user_id()
in the project_users
ACF repeater, but at the same time contains has get_current_user_id()
in favourite_users
, the correct meta query looks like this:
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'project_users',
'value' => '"'.get_current_user_id().'"',
'compare' => 'NOT LIKE'
),
),
"favorit_users" => array(
'key' => 'favorite_users',
'value' => '"'.get_current_user_id().'"',
'compare' => 'LIKE'
),
),
);
Note that unlike in your example, i’m using 'relation' => 'AND'
so that both conditions have to be met.
Also, note that multiple values ACF select fields are stored serialized in the db. That means the real value stored in the database looks like this:
| meta_id | post_id | meta_key | meta_value |
|---------|---------|---------------|--------------------|
| 14 | 8 | project_users | a:1:{i:0;s:1:"1";} |
| 20 | 11 | project_users | a:1:{i:0;s:1:"2";} |
Which means that the compare
should always be LIKE or NOT LIKE and the value should always be quoted in your case, because there will always be numbers otherwise (s:1 for string 1 character long etc.). The values you are looking for (user ids) on the other hand will be stored with quotes.
Here is the full query i ended up with:
$allProjectArgs = array(
'post_type' => 'project',
'post_status'=> 'publish',
'posts_per_page'=> -1,
'orderby' => 'favorit_users',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'project_users',
'value' => '"'.get_current_user_id().'"',
'compare' => 'NOT LIKE'
),
),
"favorit_users" => array(
'key' => 'favorite_users',
'value' => '"'.get_current_user_id().'"',
'compare' => 'LIKE'
),
),
);
$allProjectQuery = new WP_Query($allProjectArgs);