0

I would like to insert data into the wp_posts table with the inserted post_id in the 'guid' for a record that doesn't already exist. Below is the closest I've gotten but this doesn't work ($postarr['ID'] is equal to 0).

function filter_handler( $data , $postarr )
{
if($data['post_type']=='ai1ec_event'){
    $my_post_id = $postarr['ID'];
    $data['guid'] = get_option('siteurl') .'/?post_type=ai1ec_event&p='.$my_post_id.'&instance_id=';
}

return $data;
}

add_filter( 'wp_insert_post_data' , 'filter_handler' , '99', 2 );

$mypost = array(
  'post_author'     => 1,
 'post_content'     => 'Test', 
 'post_status'      => 'publish',
 'post_title'       => $_GET['assignment_name'],
 'post_type'        => 'ai1ec_event',
 'guid'     => ''
); 

$postid = wp_insert_post($mypost);

1 Answer 1

1

Why would you need that? ID in a database is auto increment - it manages it's value itself. If your target is to set custom guid, you should update the inserted post as soon as it is created via wp_update_post right after your $post_id = wp_inserted_post($mypost); like this:

...Your code above...
$postid = wp_insert_post($mypost);
$data = array(
   'ID' => $postid,
   'guid' => get_option('siteurl') .'/?post_type=ai1ec_event&p='.$postid.'&instance_id='
);
wp_update_post( $data );
2
  • Thanks for quick response. I needed this because the solution above didn't work, or at least it didn't when I tried it about an hour ago. I tried it again now and it worked. Thanks again.
    – Aori Nevo
    Feb 2, 2013 at 7:17
  • Also, I can drop the wp_insert_post_data and the filter now. Thanks again.
    – Aori Nevo
    Feb 2, 2013 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.