Change publish date from meta date when publish post

When the post is publish, I want the publish date as the same as the date in the meta data, imic_sermon_date.

Here's my code. The problem is that it get stuck when publishing a post.

function update_sermon_date ($post_id) {
    //automatically change publish date to sermon date when publish/save a postfunction update_sermon_date ($post_id) {
    $sermon_date = get_post_meta($post_id, 'imic_sermon_date', true);
    $mypost = array (
        'ID' => $post_id,
        'post_date' => $sermon_date);
    wp_update_post( $mypost );
    }

    add_action ('save_post', 'update_sermon_date');

Can someone help me with this?

Thanks.

Solutions

After searching online, I've fixed my code.

The code basically will only update when the post is edited. I added remove_action and add_action in order to prevent the code from running an endless loop .

****//automatically change publish date to sermon date when publish/save a post
function update_sermon_date () {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;


    if ( !current_user_can('edit_post', $post_id) )
        return;

    $sermon_date = get_post_meta(get_the_ID(), 'imic_sermon_date', true); 
    $mypost = array (
        'ID' => get_the_ID(),
        'post_title' => $sermon_date,
        'post_date' => $sermon_date,
        'post_date_gmt' => $sermon_date);

    if ( ! (wp_is_post_revision($post_id) || wp_is_post_autosave ( $post_id) ) ) {
        remove_action('edit_post','update_sermon_date');
        wp_update_post( $mypost );
    add_action ('edit_post', 'update_sermon_date');
    }
}

add_action ('edit_post', 'update_sermon_date');****

Similar questions

Changing new post to "pending" on publish - but "Publish failed" - why?
I only want 2 admis to be able to publish events. So I want to change any new event published by any other user to be set to "pending" when the post is "published." Which, I guess, technically, means that it won't get published. But, anyway, this is what I've tried: However, when the post is published, it gives an error that &qu...
Set Meta Value #1 for Meta Key #1 Only When Meta Value #2 and Meta Key #2 is Present
I need to make updates to my WordPress DataBase but I keep hitting a roadblock when taking into consideration a secondary condition. In very simplistic terms, I have a meta_key = 'flowers' that needs a meta_value = 'roses' to be set only where meta_key = 'month' and meta_value = 'august'. I have not been able to write a successful MySQL query that ...
Custom Wordpress Theme: Publish Date and Display Date for posts right beside each other
What's currently happening is that for posts that have an 'update' time, the original publish and editor dates are rendering side by side (with no extra information in between, or even a space for that matter), and I'm not entirely sure where I should look to fix this. I need guidance in terms of which files to look in for this output code, or, if ...
Change date format to "d M - d M Y" if start date and end date include two months
I'm using ACF repeater fields to output the dates in my WordPress site. The format I'm getting is "d - d M Y" which is what I need. But I want to change the format to "d M - d M Y" if the start date and end date fall in two months, e.g., 29th March to 5th April. How can I achieve this?
Set meta field to publish date + 2 weeks
I have a meta field called postexpiry, and I want to set the value to the publish date, + 2 weeks. So if today were October 3rd, I want the field to be set to October 17th. I was thinking about creating a hook to the publish_post filter, but I am not sure how to add 2 weeks on to get_the_date(). I know with php I can do something like this $dateInT...
Auto populate a meta box field from another meta box field when publish or save
I have a WP theme with its own custom fields. I installed the PowerPress Plugin. The PowerPress Plugin has a textfield called 'Episode Titles'. What I want is when the post is published (or save), WP will grab the data from the custom field 'Speaker' and then populated it into 'Episode Titles'. Can someone show me how to do this? I know how to popu...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .