admin管理员组文章数量:1332403
I have a code that hides images of other users. But I need to make sure that users can see their images OR images from categories.
function my_files_only( $wp_query ) {
if ( ! $_POST["action"] == "attachment" )
{
return;
}
if ( current_user_can( 'image_verified' ) )
{
return;
}
global $current_user;
$wp_query->set( 'author', "$current_user->id"); }
add_filter('parse_query', 'my_files_only' );
You can also add: $wp_query->set( 'mlo-category', "license");
But then the selection logic will be equal to AND. Therefore, we need to prescribe: relation OR. How can I fit such a request in my code?
I have a code that hides images of other users. But I need to make sure that users can see their images OR images from categories.
function my_files_only( $wp_query ) {
if ( ! $_POST["action"] == "attachment" )
{
return;
}
if ( current_user_can( 'image_verified' ) )
{
return;
}
global $current_user;
$wp_query->set( 'author', "$current_user->id"); }
add_filter('parse_query', 'my_files_only' );
You can also add: $wp_query->set( 'mlo-category', "license");
But then the selection logic will be equal to AND. Therefore, we need to prescribe: relation OR. How can I fit such a request in my code?
Share Improve this question edited Jul 9, 2020 at 18:49 mozboz 2,6281 gold badge12 silver badges23 bronze badges asked Jul 9, 2020 at 13:10 Mark DidleMark Didle 1 2- Just so I got it right, you want a query that does author = current_user OR mlo-category = "license" ? – mozboz Commented Jul 9, 2020 at 18:56
- Yes, it's right! – Mark Didle Commented Jul 9, 2020 at 19:50
1 Answer
Reset to default 1As WP_Query doesn't natively allow you to do logical OR operations (except on meta values), one option is just to run two queries to get all the Post ID's, then pass those to a new WP_Query which you can use for a loop. This is quite long and requires 3 queries, so perhaps someone will know a shorter solution
This code taken from an example here, and untested. You may have to tweak it for your use case, in particular I'm not sure what 'mlo-category' is, so you may need to convert that to a better WP_Query parameter
$postsCurrentUser = get_posts(array(
'author', "$current_user->id");
));
$postsCategory = get_posts(array(
'mlo-category', "license"
));
$mergedPosts = array_merge( $postsCurrentUser, $postsCategory ); //combine queries
$postIds = array();
// Find any post ID that is in one array or the other, don't include any twice
foreach( $mergedPosts as $item ) {
if (!in_array($item->ID, $postIds)) {
$postIds[]=$item->ID;
}
}
$args = array('post__in' => $postIds);
$q = WP_Query($args);
Let me know if that does what you want
本文标签: How to do logical OR in terms in WP Query
版权声明:本文标题:How to do logical OR in terms in WP Query? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742277792a2445521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论