Search code examples
phpwordpressuser-roles

Managing custom post backend list table based on user info.- Wordpress


I am working with a multivendor electronic shop.The website has different roles.One of the roles are shop owner.Under this role different electronic shop owner can register.like owners for whirlpool, singer , samsung...etc.

Another function is preorder.Where one customer enters into a shop and through preorder-form they can pre-order different things on that shop.

Now i have a custom post pre-order where all the pre orders are shown in backend.For an administrator he can see all pre orders from all shops i.e: preorders from whirlpool, singer , samsung.

But the problem is to the shop owner.I want to show only preorders from their respective shop.Like preorders from whirlpool to whirlpool shop owner

Is there any way to show in this way in wp admin panel under pre-order post type?


Note : i used user role editor.But its not that what i want.Also i don't want to use plugin



Solution

  • Maybe something like this:

    add_filter( 'parse_query', 'pre_order_posts_filter' );
    
    function pre_order_posts_filter( $query ) {
    
        global $post_type;      
    
        if ( 'pre-order' == $post_type && is_admin() ) {
    
            $current_user = wp_get_current_user();
    
            if ( in_array( 'shop-owner', $current_user->roles ) ) {
                // filter only for user role `shop-owner` (please adjust)
                $query->query_vars['meta_key'] = 'author_id';
                $query->query_vars['meta_value'] = $current_user->ID;               
            }
    
        }
    
    }
    

    You have to adjust it to your conditions.