Filter Orders in Admin area, based on Custom meta

Solved it, using pre_get_posts filter and WP_Query->set() method

function foo_filter_orders($query) {
    global $pagenow;
    $qv = &$query->query_vars;
    
    $currentUserRoles = wp_get_current_user()->roles;
    
    if (in_array('shop_manager', $currentUserRoles)) {
        if ( $pagenow == 'edit.php' && 
                isset($qv['post_type']) && $qv['post_type'] == 'shop_order' ) {            
            $query->set('meta_key', 'handle_by');
            $query->set('meta_value', 'shopmanager1');
        }
    }
    
    return $query;
}
add_filter('pre_get_posts', 'foo_filter_orders');

Leave a Comment

tech