Skip Navigation

[Resolved] Duplicating or Publishing a similar CPT

This support ticket is created 8 years, 9 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by benG-3 8 years, 8 months ago.

Assigned support staff: Waqas.

Author
Posts
#272823

Hi,

I have a form built with CRED which creates a CPT I call Event.

When the Event is published and the User is sent to view the single Event page, I would like to add a button which allows the User to Publish a similar Event. For example, the button will link back to the CRED form, but this time the fields will be prefilled with the same information as the Event that has just been published.

Basically, I would like the User to be able to post the event again for another date - if it's a regular Event.

I'm open to other suggestions to make this work, but I'm not the most advanced coder!

#272923

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You can achieve this as below.

1. Add following code to your theme's functions.php (at end):

add_shortcode("post-data", "post_data");
function post_data($atts) {
	$field = $atts["field"];
	
	// Fetch post id from URL parameter post_id (i.e. /?post_id=1)
	$post_id = $_REQUEST["post_id"]; 
	
	$mypost = get_post($post_id);
	$content = "";
	
	switch($field) {
		case "title":
			$content = $mypost->post_title;
			break;
			
		case "body":
			$content = $mypost->post_content;
			break;
			
		case "author":
			$content = $mypost->post_author;
			break;
			
		// and so on...
			
		default:	// Otherwise consider $field as custom field (i.e. wpcf-myfield)
			$content = get_post_meta($post_id, $field, true);
			break;
	}
	
	return $content;
}

This will create a short code [post-data] which will accept 1 parameters, Field (to retrieve) and the post id from URL parameter. See usage in 3rd step below.

2. When your form is posted and you present a button or link to go back to the previous form. Make sure that link includes post_id=<PostID> as a URL parameter, such as:

[/php]
hidden link
[/php]

Where 99 is the ID of newly (just) created post.

3. Use the short code in the "value" parameter of the CRED form field, such as:

[cred_field field="post_title" post="post" value="[post-data field='title']" urlparam=""]

[cred_field field="post_content" post="post" value="[post-data field='body']" urlparam=""]

[cred_field field="additional-info" post="post" value="[post-data field='wpcf-additional-info']" urlparam=""]

....

When the link is hit from #2 above (containing the post_id=xx in URL as Parameter), the [post-data] short code will be executed to grab the provided field='...' and short code function will fetch the post_id from URL query string.

I hope this can work smoothly.

#273798
mysca-ss1.jpg

Hi Waqas,

Many thanks for your detailed answer.

I'm getting an error with the Title field. Should I be changing any values in the code from Step One when pasting into my functions file?

This is the link for my button:

<a href="<em><u>hidden link</u></em>">Add another Event</a>

And this is my Shortcode as used for the title field in my CRED form:

[cred_field field="post_title" post="event" value="[post-data field='title']" urlparam=""]

I've also attached a screenshot of the front end error.

Thanks for your help.

#273807

Sorry Waqas, ignore that, it was a mistake my end.

How do I make this work for custom fields? This is what I tried for Entry Peice field.

in functions.php:

        case "price":
            $content = $mypost->event-entry-price;
            break;

in CRED form:

[cred_field field="event-entry-price" post="event" value="[post-data field='price']" urlparam=""]

Thanks for your help.

#273837

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Please consider using followings:

case "price":
    $content = get_post_meta($post_id, "wpcf-event-entry-price", true);
    break;

Since 'event-entry-price' is a custom field, it will not be available with $mypost. You need to grab it via get_post_meta() function. Please also notice, that if you added this custom field using Types, you may want to refer it as 'wpcf-event-entry-price'.

#275652

Thanks Waqas, it works great!

Last question(s), how do I change the code to populate a Featured Image field, and a Taxonomy field?

[cred_field field="_featured_image" value="[post-data field='featured-image']" urlparam=""]
[cred_field field="event-locations" value="[post-data field='location']"]
#275873

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Image and taxonomy fields are complex field and I am afraid those can not be pre-populated easily. That may require a lot of custom work. I will suggest to contact our Certified Partners at https://toolset.com/consultant/ for this purpose. They can help you building advanced custom code in this regard.

#277026

OK thanks Waqas.