WordPress.org

Ready to get started?Download WordPress

Forums

Immediate duplicate post with associated meta (5 posts)

  1. fanfanlat
    Member
    Posted 11 months ago #

    Hello !

    I'm trying to duplicate a post as soon as it is posted with a different post_type and with all the meta associated to the initial post.

    Here is the hook that I'm using :

    add_action( 'save_post', 'save_post_create_duplicate',10,2 );

    Here is the code for the save_post_create_duplicate function :

    function save_post_create_duplicate($post_id) {
    	global $wpdb;
    	$post = get_post( $post_id );
    	if ($post->post_type != 'specific_type') return;
    
    	$new_post = array(
    	'menu_order' => $post->menu_order,
    	'comment_status' => $post->comment_status,
    	'ping_status' => $post->ping_status,
    	'post_author' => $post->post_author,
    	'post_content' => $post->post_content,
    	'post_excerpt' => $post->post_excerpt,
    	'post_mime_type' => $post->post_mime_type,
    	'post_parent' => $post->post_parent,
    	'post_password' => $post->post_password,
    	'post_status' => 'publish',
    	'post_title' => $post->post_title,
    	'post_type' => 'another_post_type',
    	'post_name' => $post->post_name
    	);
    
    	$new_post['post_date'] = $new_post_date =  $post->post_date ;
    	$new_post['post_date_gmt'] = get_gmt_from_date($new_post_date);
            //unhook function to avoid infinite loop
    	remove_action('save_post', 'save_post_create_duplicate');
    	$new_post_id = wp_insert_post($new_post);
            /rehook the function
    	add_action('save_post', 'save_post_create_duplicate',10,2);
    
    }

    And here is the code for the other function that is supposed to duplicate the metas from the initial post to the duplicate one :

    function save_post_meta_duplicate_post($new_id,$old_id, $post) {
    	$post_meta_keys = get_post_custom_keys($old_id);
    	if (empty($post_meta_keys))
    	{
    	return;
    	}
    
    	foreach ($post_meta_keys as $meta_key) {
    		$meta_values = get_post_custom_values($meta_key, $old_id);
    		foreach ($meta_values as $meta_value) {
    			$meta_value = maybe_unserialize($meta_value);
    			add_post_meta($new_id, $meta_key, $meta_value);
    		}
    	}
    }

    And the call to this function is made just after the
    $new_post_id = wp_insert_post($new_post);

    with this code :
    save_post_meta_duplicate_post($new_post_id,$post_id,get_post($new_post_id ));

    Now the issue is that the metas are empty for the first post !
    I don't understand why... Is the hook to save_post wrong ?

    WordPress documentation says that the save_post action runs after the data is saved to the database. So, for me, the metas should already be there when I'm trying to get them.

    Thanks a lot for your advices !

  2. fanfanlat
    Member
    Posted 11 months ago #

    I've also tried to run the following SQL Query at the same place but it's not working either...

    $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->wp_postmeta WHERE post_id=$post_id");
    		if (count($post_meta_infos)!=0) {
    			$sql_query = "INSERT INTO $wpdb->wp_postmeta (post_id, meta_key, meta_value) ";
    			foreach ($post_meta_infos as $meta_info) {
    				$meta_key = $meta_info->meta_key;
    				$meta_value = addslashes($meta_info->meta_value);
    				$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
    			}
    			$sql_query.= implode(" UNION ALL ", $sql_query_sel);
    			$wpdb->query($sql_query);
    		}

    Thanks !

  3. bcworkz
    Member
    Posted 11 months ago #

    I'm not sure, but I suspect the cache is not updated when your code is executed. I'm not entirely sure how to ensure it's updated, but start by looking at Function Reference/update post caches.

  4. fanfanlat
    Member
    Posted 11 months ago #

    Thanks for your answer bcworkz !

    I've been trying to do the following :

    $post_to_be_updated = get_post($post_id, ARRAY_A);
    update_post_caches( $post_to_be_updated, 'specifi_type', false, true );

    But it's not working... :-/

    Since I'm trying to duplicate the metas immediately when I'm creating a new post, I suppose that I have to update the post only for the original post right ? Not the one I've duplicated ?

    To summary, this code is able to duplicate the post properly (creating a new record in the posts table of wordpress) but not the metas...

    And I'm guessing that the metas are not commited into the meta table when I'm executing the function that is in the hook of save_post.

    Thanks for any help !

  5. bcworkz
    Member
    Posted 11 months ago #

    It probably is nothing, but be sure the post type parameter in you update_post_cache() line is correct.

    Here's a thought, if the meta is not in the table nor the cache, perhaps it's still in $_POST? Temporarily add var_dump($_POST); die(); to your duplicate meta function and see if the needed values are there.

    The other thing is the function that triggers the 'save_post' action doesn't do anything itself about metadata, it is saved through a different mechanism. I was able to just now confirm that meta is not yet processed when 'save_post' fires. The save meta process doesn't have many convenient actions to hook. I believe the best one may be 'added_{$post_type}_meta'. The do_action() call passes the following to your callback: $meta_id, $object_id, $meta_key, $_meta_value.

Reply

You must log in to post.

About this Topic