Method to update $post_title and $post_name for custom post
-
I am creating a custom post type to idiot-proof a portion of the admin panel. I have unhooked all default entry boxes (title, excerpt, content, etc.) and have replaced them with custom fields. Using the values of some of the custom fields, I want to dynamically create/update the $post_title and $post_name (the content is hard-coded and static as a placeholder because it is irrelevant for the specific task).
I have been able to change the values using both wp_update_post and wp_insert_post, but because they run the save_post action it creates and infinite loop and adds a few hundred extra posts every time I publish or update.
Currently, I am happy with the code before //Create post object
function save_title($post_ID) { $this_post = $post_ID; global $post; $custom = get_post_custom($this_post); $event_team = $custom["event_team"][0]; $event_opponent = $custom["event_opponent"][0]; $event_date = $custom["event_date"][0]; $my_post_title = ($event_team ." vs. " .$event_opponent ." - " .$event_date); $my_post_name = ($event_team ."-vs-" .$event_opponent ."-" .$event_date); // Create post object $custom_title = array(); $custom_title['ID'] = $this_post; $custom_title['post_title'] = $my_post_title; $custom_title['post_content'] = 'This is a custom post of the sport_event type. If you can see this text, something is wrong'; $custom_title['post_name'] = $my_post_slug; // Insert the post into the database wp_insert_post( $custom_title ); }
What method can I use to pass my variables ($my_post_title and $my_post_name) to the database ($post_title and $post_name) without creating an infinite loop?
- The topic ‘Method to update $post_title and $post_name for custom post’ is closed to new replies.