Skip Navigation

[Resolved] Claim ownership of a specific post with admin approval

This support ticket is created 6 years, 5 months 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.

Tagged: 

This topic contains 1 reply, has 1 voice.

Last updated by Tim Elliott 6 years, 5 months ago.

Author
Posts
#924216

Hi Support Team

I'm trying to do something similar to what is described i this ticket:
https://toolset.com/forums/topic/author-claim-for-a-specific-post-while-submmiting-registration-form/

I have a custom post type called Tools. Users are able to claim ownership of a specific Tool, and after they have been approved they will be able to edit it.

The approval process uses another CPT called "CPT Claims".
A user fills out a form and a new custom post of post type "CPT Claim" is created.
The new post has status of "Private"
Admins can view all Private CPT claims via a view on the frontend.
The view includes 2 buttons - Reject and Approve
The reject button uses a Post Form the change the CPT Claim post status to Draft and leaves the Tool post unaltered. Therefore the CPT claim is no longer visible in the view (as it's no longer Private status).

Approve button - uses a post form to change the CPT Claim post status to Published so it will also no longer be shown in the view.
I also want it to update the post author of the Tool that has been claimed. This is the part I need help with. I'm trying to use a hook to make it update the Tool after the cred form is submitted.

I've tried to cobble together a function using other examples and the cred_save_data hook.
So far I have this:

/* After CPT Claim is approved, chenge the author of the post */
add_action('cred_save_data','set_post_author',10,1);
function set_post_author($data_fields, $form_data)
{
    list($fields,$errors) = $data_fields;
	$claimpostid = $fields['wpcf-claim-post-id'];
	$claimusername = $fields['wpcf-claim-user-name'];

	$cred_form_id = $form_data['extra_data'][0]['cred_form_id'];
	// only do this for form id 1326 - CPT Claim Approve
    if ( $cred_form_id == 1326 ) {
        if(isset($claimpostid) && isset($claimusername)) {
			// Update post 
			$my_post = array(
				'ID'           => $claimpostid,
				'post_author'   => $claimusername,
			);
			// Update the post into the database
			wp_update_post( $my_post );
		}
	}
}

Note that the claim-post-id custom field is used to store the id of the Tool within the CPT Claim post and the claim-user-name is used to store the user name of the claimant within the CPT Claim post.

But it doesn't update the post author and I'm sure I haven't got the code right.

The Approve claim for has this in it:

[credform class='cred-form cred-keep-original']

<div style="display:none;">
	[cred_field field='post_title' value='[wpv-post-title]' urlparam='' class='form-control' output='bootstrap']
	[cred_field field='claim-post-type' value='[types field='claim-post-type'][/types]' urlparam='' class='form-control' output='bootstrap']
	[cred_field field='claim-user-name' value='[types field='claim-user-name'][/types]' urlparam='' class='form-control' output='bootstrap']
  	[cred_field field='claim-user-email' value='[types field='claim-user-email' output='raw'][/types]' urlparam='' class='form-control' output='bootstrap']
    [cred_field field='claim-post-id' value='[types field='claim-post-id' output='raw'][/types]' urlparam='' class='form-control' output='bootstrap']
</div>
	[cred_field field='form_submit' value='Approve' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

Hope you can help fix this.
Thanks
Tim

#924331

Hi Team

I've managed to find a solution eventually...

/* After CPT Claim is approved, chenge the author of the post */
add_action('cred_save_data','set_post_author',10,2);
function set_post_author($post_id, $form_data)
{

	$claimpostid = get_post_meta($post_id, 'wpcf-claim-post-id', true);
	$claimusername = get_post_meta($post_id, 'wpcf-claim-user-name', true);
	$user = get_user_by('login',$claimusername);
	
	// only do this for form id 1326 - CPT Claim Approve
    if ($form_data['id'] == 1326) {
        if(isset($claimpostid) && isset($claimusername)) {
			// Update post 
			$my_post = array(
				'ID'           => $claimpostid,
				'post_author'  => $user->ID,
			);
			// Update the post into the database
			wp_update_post( $my_post );
		}
	}
}