• Hi all,
    Need to modify post query in admin panel to receive custom list of posts (limited by custom meta_key)

    ex. Admin will assign custom manager to post (meta_key – manager_id) and manager must only see only this posts. (Manager has custom user role manager)

    So how can i do this? (I definitely can modify wordpress core files, but that’s a bad solution)

Viewing 1 replies (of 1 total)
  • Hi,
    Naturally, you should not modify core files. It should be possible to do this using a plugin or by adding some code in the functions.php of your (child) theme.

    Here is an example for you to start with:

    function show_only_allowed_posts( $query ) {
    	if ( $query->is_admin ) {
    		$query->set('meta_key',  'manager_id' );
    		$query->set('meta_value',  '123' );
    	}
    
    	return $query;
    }
    add_filter('pre_get_posts', 'show_only_allowed_posts');

    This will add the custom parameters for all WP_Queries executed in the admin area (basically it will show posts where the manager_id custom field is set to 123). Please note that this code wasn’t tested but hopefully it will point you in the right direction.

    This also won’t prevent users from seeing/editing the posts by creating the URL manually. You will need additional code to block access completely.

    Hope that helps.

Viewing 1 replies (of 1 total)

The topic ‘Custom post query in admin panel’ is closed to new replies.