• I have created a custom post type (sc_products), I have also created a new user role.

    Administrators should be able to view all products added by any user and be able to modify these.

    New users of my new role should only see products they add.

    When a new user logs in and goes to the product list page (/wp-admin/edit.php?post_type=sc_products), they see a list of all products and can enter each product but not update them.

    HOWEVER, once the user adds a product of their own, they then only see their own product in the product list page.

    Is there a capability that will not show the full product list uploaded by all other users before they add their own product?

    Thanks

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

    (@candell)

    EDIT/UPDATE

    I have just noticed that when the user has no product, above the Bulk Actions and All dates dropdowns are the filters

    All | Published

    When the user add a product of their own, this now shows

    All | Mine | Published

    and shows just their own, which is why I am seeing what I seeing in my initial post. They can always select all to see all users posts.

    How do I remove this ALL view, so if they have no posts, it will show 0 and not all?

    Moderator bcworkz

    (@bcworkz)

    The “views_{$this->screen->id}” filter can be used to unset the “All” element. In your case $this->screen->id should be “edit-sc_products”. Only do so for appropriate roles of course.

    This removes the link, but someone could still manually type in the correct URL and obtain the view anyway, though they can still only alter their own products. If it’s important to securely hide other products, use the “pre_get_posts” action to alter the table query just as would be done for any post query. Apply appropriate logic to only alter the query you need to affect — a certain role requesting all sc_products in an admin view. Add a query var restricting the query to products where the current user is the author. Thus the All view would be the same as the Mine view. If you do this, the above mentioned filter isn’t all that necessary, but probably still helps with UX.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Post Type with new user role oddity’ is closed to new replies.