Perform function on publish AND save (not just save)
I am using this function to auto-set a post title based on a couple of custom fields:
function set_event_title( $data , $postarr ) {
if($data['post_type'] == 'event') {
$getdate = get_post_meta($postarr['ID'],'event_datestart',true);
$dateformat = new DateTime($getdate);
$date = $dateformat->format( 'm-d-Y' );
$event_venue = get_post_meta($postarr['ID'],'venue_name',true);
$event_title = $event_venue . ' - ' . $date;
$post_slug = sanitize_title_with_dashes ($event_title,'','save');
$post_slugsan = sanitize_title($post_slug);
$data['post_title'] = $event_title;
$data['post_name'] = $post_slugsan;
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'set_event_title' , '99', 2 );
But I can only get it to work on post update and not on the initial publishing of the post. Is there a way to make this work both the FIRST time as well as the rest of the times when the post is saved?
Solutions
Instead of taking meta values from get_post_meta function take it from $postarr array because when initial publishing of the post there isn't any values for event_datestart and venue_name in database and it returns empty string.
Updated Code :
function set_event_title( $data , $postarr ) {
if($data['post_type'] == 'event') {
$getdate = $postarr['event_datestart']; // Replace event_datestart with the input field name of event_datestart post meta.
$dateformat = new DateTime($getdate);
$date = $dateformat->format( 'm-d-Y' );
$event_venue = $postarr['venue_name']; // Replace venue_name with the input field name of venue_name post meta.
$event_title = $event_venue . ' - ' . $date;
$post_slug = sanitize_title_with_dashes ($event_title,'','save');
$post_slugsan = sanitize_title($post_slug);
$data['post_title'] = $event_title;
$data['post_name'] = $post_slugsan;
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'set_event_title' , '99', 2 );
For more information see this page.
Similar questions
Perform a function when a user clicks register button
I am kind of new to WordPress coding. I'm trying to edit the wp-login.php file so if a user clicks the register button, it registers the user and stores the information in a different database table. My code to insert new data into the database is below. Update - From what you said, I tried the code below, but no data was inserted into the database...
perform a function when a user clicks register button in wordpress
am kind of new to wordpress coding, am trying to edit the wp-login.php file, in which if a user clicks register button, it registers the user and stores an information on a different table in the database.. my code to insert new data into the database is below, how do i add it inside wordpress? i added this lines of code based on what i read about ...
Perform JS function on all ID's on a page
This code is overriding a conflict between fullpage.js and WP Hero Slider I have two pages with two sliders. This code is being called successfully on the first slider, but not the second. I need to refactor the code in order to make sure the JS function is called on the second slider, which has the same ID .hslider_misc_holder, not just the first....
Perform PHP function on radio button click, Wordpress
I have two radio buttons in a wordpress file. I have two contact-form-7 shortcodes and want to execute one if the radio is clicked "Yes" and the other if the radio button is "No". I am willing to use ajax as i don't want the page to reload but have no idea how to use it. It will be great if you could guide me. Example HTML:
creating jquery function that redirects focus to input if blank, otherwise perform normal search
i am working on a jquery script for normal wordpress search form, where if user does not type anything in search the focus() goes to the input field and stops the search to happen, otherwise let the search run normally when something is typed in. i have the following script that works for the if check and if nothing is typed in, the focus() is redi...
Wordpress customizer preview scripts perform live preview on initial page load and not while I navigate inner pages from the preview window
Thanks for stopping by. I'm working on a wordpress theme with custom customizer controls. Everything works fine so far. I'm also adding a live preview so users see what they change This seems to work fine. The issue I have is that when I click on any link (on the navigation menu for instance) within my preview window, the live preview seems to stop...