Forums

add new post during schedule and save with wp_insert_post (1 post)

  1. evossman
    Member
    Posted 1 year ago #

    OK, I've searched far and wide and have tried a million "solutions" from Sunday but can't plug and chug through the right combo.

    The short of what I want to do and I am open to all solutions is that when a post (call it postone) changes from draft_to_future or pending_to_future I want to take some custom fields from postone and insert a new post in the database with the post_status = future, post_content = postone->post_content, the post_date equal to a date pulled form postone custom field.

    I've tried a lot of combinations using wp_update_post, wp_insert_post, etc, but can't get one to stick. My best stab at it is outlined below.

    Here's what I'm doing and trying.

    I have a custom post type and associated with each custom posttype I have date and time information saved with the post under post meta. Call these date1, date2, date3 and so on. All of these dates will be in later dates than the current date and time that i'm writing the post.

    Question 1. Now, when I save a draft of this post I'd like take the first date, date1 and make the current post have that date. I am currently doing this with add_filter( 'wp_insert_post_data'...

    function dodate_post_data( $data, $postarr )
    {
    
        $theID = $postarr['ID'];
        $thecurrentID = $data['ID'];
    
        $my_post_date = get_post_meta($theID, '_start_one_year', true).'-'.get_post_meta($theID, '_start_one_month', true).'-'.get_post_meta($theID, '_start_one_day', true).' '.get_post_meta($theID, '_start_one_hour', true).':'.get_post_meta($theID, '_start_one_min', true).':00';
         $gmttt = "$my_post_date";
         $newgmt = strtotime($gmttt);
         $offset = 10*3600;
         $newgmt = $newgmt + $offset;
        if (($data['post_type'] == 'events')) {
    		$data['post_date'] = $my_post_date;
                    $data['post_date_gmt'] = date('Y-m-d H:i:s', ($newgmt));
    
        }	
    
    	return( $data );
    }
    add_filter( 'wp_insert_post_data' , 'dodate_post_data' , 99, 2 );

    that works good, except it only works the second time i save. My assumption is this filter works after the save_posts function to save my date1, date2, etc that I have running.

    Help on this would be great but leads us to the next issue...

    2. After the data is saved as a draft or submitted as pending (by one of my authors) I'd like to take date1, date2, date3 and so on, and create a new post with it. My thought, and what I've tried, is to take the transition_post_status and hook into it.
    add_action('draft_to_future', 'do_this');

    where do_this takes the $post data and uses wp_insert_post to add a new post with the status of future so it shows up as a scheduled post. My current function do_this

    function do_this($post){
    global $post;
    $theID = $post->ID;
    $oldpost = $post;
        $new_event_date = get_post_meta($theID, '_start_two_year', true).'-'.get_post_meta($theID, '_start_two_month', true).'-'.get_post_meta($theID, '_start_two_day', true).' '.get_post_meta($theID, '_start_two_hour', true).':'.get_post_meta($theID, '_start_two_min', true).':00';
         $gmttt = "$new_event_date";
         $newgmt = strtotime($gmttt);
         $offset = 10*3600;
         $neweventgmt = $newgmt + $offset;
    $newpostargs = array(
    		'post_parent' => $post->ID,
    		'post_status' => 'future',
    		'post_type' => 'events',
    		'post_date' => $new_event_date,
    		'post_date_gmt' => $neweventgmt,
    		'post_title' => $post->post_title,
    		'post_content' => $post->post_content);
    global $newpostid;
    $newpostid = wp_insert_post($newpostargs);

    This works ish but here are the problems. The newly inserted post gets saved with the current date as post_date, a null date as post_date_gmt, and all of the custom fields that were on the original post are now save on this post. The post gets "published" status, but without an actual published date (very odd behavior).

Topic Closed

This topic has been closed to new replies.

About this Topic