Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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 );
share|improve this answer
    
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. –  amn Feb 2 '13 at 7:17
    
Also, I can drop the wp_insert_post_data and the filter now. Thanks again. –  amn Feb 2 '13 at 7:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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