I have built a directory where logged in users can modify there own content. I would like the option of allowing them to make the post associated with their directory listing private (so not showing on the directory). I was wondering what might be the best way to achieve this? I tried adding a single checkbox to indicate that the listing was to be private and then a little code to implement that. This solution is not working for me though... maybe there might be a better way?
// Check if the custom field "hide-from-directory" has a value of "1"
if(get_post_meta($mp_post_id, 'hide-from-directory', true) == 1 ) {
// Set the post status to private
$mp_post_args = array( 'ID' => $mp_post_id, 'post_status' => 'private' );
wp_update_post($mp_post_args);
return;
}
I've changed the code you added to functions.php file as given under:
function set_private_when_hide_from_directory( $post_id, $form_data ) {
if ($form_data['id']==95 ) {
// Get the value of the custom field "wpcf-hide-from-directory-2"
$hide_from_directory = get_post_meta( $post_id, 'wpcf-hide-from-directory-2', true );
// Check if the custom field "hide-from-directory" has a value of "1"
if( $hide_from_directory == 1 ) {
// Set the post status to private
$post_args = array( 'ID' => $post_id, 'post_status' => 'private' );
wp_update_post( $post_args );
}
}
}
add_action( 'cred_save_data', 'set_private_when_hide_from_directory',10,2);
- Types custom field saved with prefix "wpcf-" so if your custom field slug is "hobby" then the meta key will be "wpcf-hobby"
- 95 is the form ID
Amazing... works a treat. Just one small problem. When a CPT is published the user can usually go to /my-profile/ where they can edit their profile. Once the CPT becomes private they can no longer edit their profile in this view... is there a way of allowing that?
As per our support policy - we entertain only one question per ticket. As the issue reported with this ticket is resolved.
May I kindly ask you to open a new ticket with every new question you may have. This will help other users searching on the forum as well as help us to write correct problem resolution summery.