Skip Navigation

[Résolu] Retain checkbox and relationship value when duplicate post on front end

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Client uses some custom code to duplicate posts, which works correctly except for checkboxes fields. How to handle duplicating checkboxes fields onto the new posts?

Solution:
Checkboxes fields are stored as serialized arrays in wp_postmeta, and there is an anomaly with WordPress in how the value is returned by get_post_meta, depending on whether you retrieve the value of the field directly (by specifying the key), where it is returned as an array, or whether you retrieve the value of all fields at once (by not specifying a key), where it is returned as a string.

I updated the code used by the client to use the function maybe_unserialize to update the value before saving the post meta on the new, duplicated, post, as shown below: https://toolset.com/forums/topic/retain-checkbox-and-relationship-value-when-duplicate-post-on-front-end/#post-948285

This support ticket is created Il y a 6 années et 4 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Ce sujet contient 1 réponse, a 2 voix.

Dernière mise à jour par Nigel Il y a 6 années et 4 mois.

Assisté par: Nigel.

Auteur
Publications
#947725

I have managed to create front end post duplication with guidannce from https://toolset.com/forums/topic/front-end-user-duplicate-post-function/. However the values in check boxes and the new relationship linking would not be duplicated, and left blank instead in the duplicated post.

How should I resolve this? Thanks.

#948285

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Hi there

Checkboxes fields are stored as serialized arrays in wp_postmeta, and I noted an anomaly in how the value is returned by get_post_meta, depending on whether you retrieve the value of the field directly (by specifying the key), where it is returned as an array, or whether you retrieve the value of all fields at once (by not specifying a key), where it is returned as a string.

I updated the code to use the function maybe_unserialize to update the value before saving the post meta on the new, duplicated, post, like so:

function tssupp_duplicate_post( $post_id, $form_data ) {

	if ( $form_data['id'] == 9 ) { // Edit form ID

	    // get data of original post
	    $post = get_post( $form_data['container_id'], ARRAY_A );

	    // update the new post with this data
	    $post['ID'] = $post_id;
	    $post['post_title'] = 'Copy of ' . $post['post_title'];
	    wp_update_post( $post );

	    // get fields of original post
	    $fields = get_post_meta( $form_data['container_id'] );

	    // update the new post with these fields
	    foreach ($fields as $key => $values) {
	        foreach ($values as $value) {   

	        	$value = maybe_unserialize( $value );

	            add_post_meta($post_id, $key, $value, false);
	        }
	    }

	}
}
add_action( 'cred_save_data', 'tssupp_duplicate_post', 10, 2 );

If the posts in question that you are duplicating have related posts created with Types relationships and you want to duplicate the relationships too, then you would need to update the above code to use the relationships API to retrieve the relationship data from the source post and then set up the same on the duplicated post.

You will need to use the functions toolset_get_related_posts to retrieve the relationship data, and toolset_connect_posts to set the same on the duplicate.

These functions are described here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

The specifics will depend on your relationships, but if you get stuck let me know.