Skip Navigation

[Closed] How to add "read_private_posts" to custom post type

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1578793

Tell us what you are trying to do?

I have created several custom post types with Toolset, and I need the capability that is equivalent to "read_private_posts". I need to create a special role that can only preview a "Private" custom post, without being able to edit the post.

I can't use the "Preview Any" capability provided with Toolset, because that capability appears to only work if the user also has edit privileges.

Is there any documentation that you are following?

No. I have tried these approaches so far. For example, with two custom post types named 'donate' and 'podcast':

- The register_post_type_args filter

<pre>

add_filter('register_post_type_args', 'add_caps_custom_post_types', 10, 2);

function add_caps_custom_post_types( $args, $post_type ) {
switch ( $post_type ) {
case "donate":
$args['capabilities']['read_private_posts'] = 'read_private_donate';
break;
case "podcast":
$args['capabilities']['read_private_pages'] = 'read_private_podcast';
break;
}
return $args;
}
</pre>

- I have also tried a similar function with the wpcf_type filter, which appears to be called in types/library/toolset/types/embedded/includes/custom-types.php
on the register_post_type() call that creates new custom post types with Toolset.

With either approach, I do seem to be able to attach this data to the custom post type, but it does not result in a capability that I can assign to a role.

I suspect I may just be ignorant of exactly what I need to tell $args in order to add a capability. Can you help?

Note: I saw a 2012 thread here https://toolset.com/forums/topic/feature-request-include-capability_type-on-type-def/ that seemed like it might be relevant. But that feature doesn't seem to have been implemented, otherwise I would see capabilities like "read_private_donate" automatically.

Thank you!

#1578913
Screen Shot 2020-04-08 at 2.53.13 PM.png

In Toolset Access advanced settings you can add custom capabilities to any custom role. For a custom post type with the slug "donate", you would add the custom capability "read_private_donates", or for the post type slug "podcast" you would add "read_private_podcasts". I'm not sure how that would be handled in custom code, and that's outside the scope of support we provide here, but most likely you need the "s" on the end of the post type slug since that's the standard. Here's a screenshot showing how this would be accomplished for a post type "book" in Access Controls.

#1581211

Hi Christian! Thanks for this support.

I did try adding "read_private_donates" and "read_private_podcasts" as you describe.

I tried doing this without adding any custom code.

Unfortunately, this did not work for me. When I try to view my private examples of these custom post types, while logged in as the user with this custom role, I usually got a 404 "page not found" response.

Since Toolset Types offers the ability to create custom post types, it seems to me that supporting the "read private" capability, which is standard with the built-in WordPress posts and pages, should be within the scope of Toolset support, and clearly documented.

It's possible that my site has some complex code that is breaking something here, and I don't expect you to help me figure that out.

But can you confirm that, on a clean install of WordPress and Toolset Types and Toolset Access Control, you are able to:

- Create a custom post type, e.g. "book"
- Create a custom role
- Give this custom role a custom capability, "read_private_books"
- Create a test post of type "book" and publish it as Private
- While logged in as your custom role, you can view this private post, but not edit it.

If all that works for you, then I will consider this resolved and keep digging to resolve the problems of my own particular site.

But I want to be clear that this does work, because Toolset Types should support this as part of creating custom post types.

Thank you!

#1583293
post-type-control.png
donate-role.png
donate-role-dashboard.png
donate-private-logged-in-podcast-role.png
donate-private-logged-in-donate-role.png
donate-private-editor-donate-role.png
donate-private-editor-admin.png
donate-capability.png

I think the scenario you described is working in a sandbox site I created for our collaboration, so please confirm that I haven't overlooked something. For reference, I will attach some screenshots here. Others who find this ticket won't have access to the sandbox site so these may be useful.
- donate-role.png: Setting up a new custom role for the Users who will have the ability to read private Donate posts.
- donate-capability.png: The custom capability read_private_donates is granted to the PrivDon role.
- post-type-control.png: See the Access Control settings for the Donate post type. Note the PrivDon role has read access.
- donate-role-dashboard.png: View of the wp-admin dashboard when logged in as PrivDon User. Note the Donate post type is not visible in the main menu.
- donate-private-editor-admin.png: The private Donate post editor screen as seen by the admin User
- donate-private-editor-donate-role.png: The same private Donate post editor page when logged-in as the PrivDon User
- donate-private-logged-in-donate-role.png: Front-end display of the private Donate post when logged-in as PrivDon User
- donate-private-logged-in-podcast-role.png: Front-end display of the same private Donate post when logged-in as PrivPod User or logged-out.

I will send a private message to you containing the login information for the various User roles in a moment.

#1627135

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

The topic ‘[Closed] How to add "read_private_posts" to custom post type’ is closed to new replies.