Custom Post Type by user

As per what I have understood is you have a Custom Post type as event and user role like Secretary.

Now if a Secretary logged in , he can only be able to see his own events added, not the other one.

So if the above is correct so for that, we will use pre_get_posts filter

function wp_list_mine_posts( $wp_query_obj ) {
    // Front end, do nothing
    if( !is_admin() )
        return;

    global $current_user, $pagenow;
    get_currentuserinfo();

    // http://php.net/manual/en/function.is-a.php
    if( !is_a( $current_user, 'WP_User') )
        return;

    // Not the correct screen, bail out
    if( 'edit.php' != $pagenow )
        return;

    // Not the event post type, bail out
    if( 'event' != $wp_query_obj->query['post_type'] )
        return;

    // If the user is not administrator, filter the post listing
    if( ! current_user_can( 'delete_plugins' ) )
        $wp_query_obj->set( 'author', $current_user->ID );
}

add_action( 'pre_get_posts', 'wp_list_mine_posts' );

So for this, only administrator can see all the events added by all users.
And if Secretary logged in then he can only be able to see his own events added.

Leave a Comment