Does anyone know of a way to add code to the functions.php file which will automatically force all posts belonging to a custom post type to be “private” and/or “password protected” with a default password set?
I am specifically referring to creating a new post or editing an existing post thus ensuring a post belonging to a specific custom post type never changes…
You can hook onto
save_post
,wp_insert_post
orwp_insert_post_data
to modify the post object prior to it being inserted or saved.Using
save_post
orwp_insert_post
the callback would need to declare two args and would receive the post object as the second incoming variable.. (and i’m showing you to cover the alternatives, TheDeadMedic’s example would be fine to).For setting default values for a particular post type for new posts you can use a small hack by hooking onto
default_content
(althoughdefault_title
would also work to), like the example i gave here.You essentially need two functions, one to modify post objects at the time of save/insert and one to set default post object values, here’s an example of the two necessary functions(again noting you can swap my save_post callback for the example already given by TheDeadMedic).
Hope that helps…
Couldn’t we just make a template page for the custom post type and do a check if the is the user is logged in and check if the user is a certain role? Lets take for example you want to have a post-type that is only to be viewed by the admins:
There are three hooks specifically for when the status of a post changes:
How to use:
In this case, the
portfolio
post type will always be published privately. However, it is possible to create a draft, as this is not restricted.Note that since this is not a filter, you must use the
wp_update_post()
function to update the status.