2

enter image description here

I'd like to remove some posts shown for some users.

Here's the long problem explanation:

In my site, there are Groups. These groups can post blog posts, but only access blogposts made by their own chapter.

So I put a meta down for users and another in the posts, but now, in the posts list, I'd like to only show those where the 2 metas match.

So how can I remove some of the blog posts from the blog listing page in the Administration?

1 Answer 1

2

This could done with couple of ways:

With pre_get_posts Small modification will need it like the actual meta value names

function alter_edit_php_query_remove_posts($query) {
    $screen = get_current_screen();
    if(is_admin() && $screen->post_type == 'post') {

        $current_user = wp_get_current_user();
        $key = 'nickname';
        $single = true;
        $user_meta_value = get_user_meta( $current_user->ID, $key, $single); 

        $meta_query = array(
                        array(
                            'key'=>'_edit_last',
                            'value'=>$user_meta_value,
                            'compare'=>'=',
                        ),
        );
        $query->set('meta_query',$meta_query);
  }
}
add_action('pre_get_posts','alter_edit_php_query_remove_posts');

With map_meta_cap:

    add_filter('map_meta_cap', 'prevent_user_view_post', 3, 4 );
    function prevent_user_view_post( $required_caps, $cap, $user_id, $args ){   

        if( ( $cap=='read_post'|| $cap='edit_post') ){
            $post_id  = $args[0]??0;

            $post_meta_value=get_post_meta($post_id,'meta_key',true);
            $key = 'nickname';
            $single = true;
            $user_meta_value = get_user_meta( $user_id, $key, $single );
            $user_meta_value = 2;
            if ( $post_meta_value != $user_meta_value && $post_id ==1 ){
                $required_caps[] = 'do_not_allow';
            }

        }

        return $required_caps;

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.