0

I have a frontend form through which users can create custom posts(events in my case).

Now I am want to fetch all address data from that form and create a new post once that complete form is submitted by the user from the front end.

I am trying to use save_post to trigger my function in function.php.

But below function gives 500 error in console and the page is frozen.

add_action( 'save_post','add_menu_custom_event');

function add_menu_custom_event($post_id) {
    //Check it's not an auto save routine
          if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
                return;

    //Perform permission checks! For example:
          if ( !current_user_can('edit_post', $post_id) ) 
                return;
echo "post_id".$post_id;

$data = array (
     'post_title'    => 'New Title6',
     'post_type' => 'tribe_venue', 
     'meta_input'   => array (
          '_VenueAddress' => 'Address check', 
          '_VenueCity' => 'city check',
          '_VenueCountry' => 'Afghanistan',
          '_VenueProvince' =>  'province',  
          '_VenueZip' => '121007',
          '_VenuePhone' => '7503118112',
          '_VenueURL' => 'someurl.com',
          '_VenueShowMapLink' => 1,
     ),

);




//      save the new post
 $pid = wp_insert_post($data,true);  // gives 1536    _EventVenueID

 update_post_meta( $post_id, '_EventVenueID', $pid ); // i am trying to link parent post id to its child post id

}
0

i GOT THE issues , here is my answer.

Now two posts are linked properly. I just need to update the metadata dynamically.

add_action( 'save_post','add_menu_custom_event', 10, 3);

function add_menu_custom_event(  $post_id, $post, $update  ) {

     /*
     * In production code, $slug should be set only once in the plugin,
     * preferably as a class property, rather than in each function that needs it.
     */

     //Check it's not an auto save routine
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
      return;

    //Perform permission checks! For example:
    if ( !current_user_can('edit_post', $post_id) ) 
      return;

    //if( false != $update )
     //d(get_current_screen( ));
     /d($update);
     //die; 

    $post_type = get_post_type($post_id);

$_REQUEST['']

    // If this isn't a 'book' post, don't update it.
    if ( "tribe_events" != $post_type ) return;

    $data = array (
         'post_title'    => 'New Title6'.rand(1, 9999),
         'post_type' => 'tribe_venue', 
         'meta_input'   => array (
              '_VenueAddress' => 'Address check', 
              '_VenueCity' => 'city check',
              '_VenueCountry' => 'Afghanistan',
              '_VenueProvince' =>  'province',  
              '_VenueZip' => '121007',
              '_VenuePhone' => '7503118112',
              '_VenueURL' => 'someurl.com',
              '_VenueShowMapLink' => 1,
         ),

    );

  $pid = wp_insert_post($data,true);  

  update_post_meta( $post_id, '_EventVenueID', $pid );

}

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.