admin管理员组文章数量:1415460
I have this list of posts in wordpress. I've made a favorite function that when you mark something as favorite it gets added to the front of the line. The issue here is I run this 2 place. One top panel which shows my favorite posts that I'm attached to and then the second post that shows my favorized posts from other users. Top panel works just fine.. But the issue is that my profile cards are shown in the second panel where other users cards are shown:
As you can see... I have a duplicate in the second panel. How do I get rid of it using WP_Query?
Users are inside a multiple user field with ACF Fields.
I've tried doing all kinds of stuff such as AND and ORs. I am mostly likely close to the solution, but I can't seem to find the solution..
TOP Panel code:
$aProjectArgs = array(
'post_type' => 'project',
'post_status'=> 'publish',
'posts_per_page'=> -1,
'orderby' => 'favorit_users',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'project_users',
'value' => get_current_user_id(),
'compare' => '='
),
array(
'key' => 'project_users', // name of custom field
'value' => '"'.get_current_user_id().'"',
'compare' => 'LIKE'
),
),
array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'project_users',
'value' => get_current_user_id(),
'compare' => '='
),
array(
'key' => 'project_users', // name of custom field
'value' => '"'.get_current_user_id().'"',
'compare' => 'LIKE'
),
),
"favorit_users" => array(
'key' => 'favorite_users',
'value' => get_current_user_id(),
'compare' => 'LIKE'
),
),
),
);
$aProjectQuery = new WP_Query($aProjectArgs);
Second panel (LOWER):
$allProjectArgs = array(
'post_type' => 'project',
'post_status'=> 'publish',
'posts_per_page'=> -1,
'orderby' => 'favorit_users',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'project_users',
'value' => get_current_user_id(),
'compare' => '!='
),
array(
'key' => 'project_users',
'value' => '"'.get_current_user_id().'"',
'compare' => 'NOT LIKE'
),
array(
'key' => 'favorite_users',
'compare' => 'NOT EXISTS'
),
),
"favorit_users" => array(
'key' => 'favorite_users',
'value' => $iCurrentUserID,
'compare' => 'LIKE'
),
),
);
$allProjectQuery = new WP_Query($allProjectArgs);
In the end the duplicate should be hidden in the second panel. Pretty much every card I am attached to should be hidden. Cards with my favorite (stars) should stay IF it is not related to my user.
I have this list of posts in wordpress. I've made a favorite function that when you mark something as favorite it gets added to the front of the line. The issue here is I run this 2 place. One top panel which shows my favorite posts that I'm attached to and then the second post that shows my favorized posts from other users. Top panel works just fine.. But the issue is that my profile cards are shown in the second panel where other users cards are shown: https://gyazo/75cf45a0844cba1f9ca6818e059d8bf6
As you can see... I have a duplicate in the second panel. How do I get rid of it using WP_Query?
Users are inside a multiple user field with ACF Fields.
I've tried doing all kinds of stuff such as AND and ORs. I am mostly likely close to the solution, but I can't seem to find the solution..
TOP Panel code:
$aProjectArgs = array(
'post_type' => 'project',
'post_status'=> 'publish',
'posts_per_page'=> -1,
'orderby' => 'favorit_users',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'project_users',
'value' => get_current_user_id(),
'compare' => '='
),
array(
'key' => 'project_users', // name of custom field
'value' => '"'.get_current_user_id().'"',
'compare' => 'LIKE'
),
),
array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'project_users',
'value' => get_current_user_id(),
'compare' => '='
),
array(
'key' => 'project_users', // name of custom field
'value' => '"'.get_current_user_id().'"',
'compare' => 'LIKE'
),
),
"favorit_users" => array(
'key' => 'favorite_users',
'value' => get_current_user_id(),
'compare' => 'LIKE'
),
),
),
);
$aProjectQuery = new WP_Query($aProjectArgs);
Second panel (LOWER):
$allProjectArgs = array(
'post_type' => 'project',
'post_status'=> 'publish',
'posts_per_page'=> -1,
'orderby' => 'favorit_users',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'project_users',
'value' => get_current_user_id(),
'compare' => '!='
),
array(
'key' => 'project_users',
'value' => '"'.get_current_user_id().'"',
'compare' => 'NOT LIKE'
),
array(
'key' => 'favorite_users',
'compare' => 'NOT EXISTS'
),
),
"favorit_users" => array(
'key' => 'favorite_users',
'value' => $iCurrentUserID,
'compare' => 'LIKE'
),
),
);
$allProjectQuery = new WP_Query($allProjectArgs);
In the end the duplicate should be hidden in the second panel. Pretty much every card I am attached to should be hidden. Cards with my favorite (stars) should stay IF it is not related to my user.
Share Improve this question asked Sep 3, 2019 at 11:36 LatoLato 11 Answer
Reset to default 1Hopefully 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);
本文标签: phpHide posts if user is added to it WPquery
版权声明:本文标题:php - Hide posts if user is added to it WP_query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745190129a2646873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论