Skip Navigation

[Resolved] Allow users to make their content private

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by andrewH-12 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2533133

Hi,

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;
    }
#2533429

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share bit more details where exactly you added the checkbox and what hook you are using that should run the code you shared.

Are you using Toolset form? if yes, can you please share problem URL where you added the form.

I have set the next reply to private which means only you and I have access to it.

#2533545

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Toolset form's offers the hook "cred_save_data" which you can uses to update the post content on fly.

More info:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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

Can you please check now if it works as expected.

In addition to that - Toolset also offers the "Custom code" section so if you want you can move the above code which is added to functions.php file to "Custom code" section.
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

#2533619

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?

#2533667

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2533669

My issue is resolved now. Thank you!