Support » Plugin: Post Expirator » Not working if Posts inserted with ‘wp_insert_post’ and postexpirator shortcode

  • Resolved davidduckwitz

    (@davidduckwitz)


    If i create posts with “wp_insert_pos”t and write the Shortcode to “post_content”, expiration is not working.

    i used these Shortcode:
    [postexpirator type=”full” “2018-08-23 15:29:38”]

    Maybe my Shortcode is not right?

    When I open the created post (edit) and save it, then it’s working…

    any Ideas how i can create posts from “wp_insert_post” and they will expire after 28 days?
    Thanks a lot

Viewing 2 replies - 1 through 2 (of 2 total)
  • davidduckwitz

    (@davidduckwitz)

    I’ve found Solution by myself.
    Here’s what i’ve done, maybe it can help other Developers, too:

    1. Define you date when post should be expire (in my case now +28 days):

    $date = new DateTime('now +28 days');
    $expires = date_format($date, 'Y-m-d H:i:s');

    2. Create Post with wp_insert_post and get post ID

    // Build Post Array
    $my_post  = array(
    	'post_author' => wp_strip_all_tags($user_id),
    	'post_category' => array( $cat ),
    	'post_content' => $postContent,
    	'post_content_filtered' => '',
            'post_title' => $value->name,
    	'post_excerpt' => '',
    		'post_status' => 'publish',
    		'post_type' => 'post'			
    	);
    			
          // Insert the post into the database and get ID
    	$post_id = wp_insert_post( $my_post );

    3. Add Post Metas for eypiration (delete Posts if expired)

    //create Post Meta (In this Case it's the expiration date for postexpire)
    				
    $ts = get_gmt_from_date("$expires",'U');
    update_post_meta($post_id, '_expiration-date', $ts);
    			
    $opts['expireType'] = "delete";
    $opts['id'] = $post_id;			
    update_post_meta($post_id, '_expiration-date-options', $opts);
    			
    update_post_meta($post_id, '_expiration-date-status','saved');

    Best Regards from Germany

    Many Thanks for posting this solution David.
    Your code has worked for me and saved me a lot of time. Much appreciated

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.