• Hi, I have two custom post types which I’ve set up to have authors. Now, is it possible to make it so that members can only view the posts that they have authored in the admin menu?
    I imagine pre_get_posts will get into play, but I can’t think of a way to implement it.

    To be more clear, I need to filter the results in “edit.php?post_type=my_cpt”.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter vslavov

    (@vslavov)

    I almost found out how to deal with the problem. I’ve written this function:

    function myfilter($query) {
    if (isset($_GET[‘post_type’]) && post_type_exists($_GET[‘post_type’]) && in_array(strtolower($_GET[‘post_type’]), array(‘dx_invoice’, ‘here’))) {
    if(!is_admin() ) {
    global $user_ID;
    $query->set(‘author’, $user_ID);
    }
    }
    return $query;
    }
    add_filter(‘pre_get_posts’, array( $this, ‘myfilter’));

    Now the only problem is whenever I browse the CPT listing (edit.php?post=my_cpt) I can see three links: Mine | All | Published, the page defaults to “Mine”, but All and Published are still accessible, any idea how to prevent that?

    Moderator bcworkz

    (@bcworkz)

    Yes! Hook the “views_edit-{$post_type}” filter and unset the unwanted keys in the passed array. Where $post_type is the name of your CPT. Be sure to only do this when appropriate, or it will be done for everyone’s screen, even admins.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Only view CPT posts authored by current user’ is closed to new replies.