Replace Post’s publish date with a MetaBox custom field value (date picker)

AnLip
9 Aug 2022

If for any reason you need the Post’s publish date to be replaced by the value of a date-picker custom field. This has been made to work with MetaBox plugin, I cannot guarantee it will work easily with ACF or other equivalents.

I used it because WP natively orders posts by publish date. On a new project I wanted all posts of a certain CPT based on dates (exhibitions for an artist-painter) to be ordered chronologically even though they were going to be created in a random order (client would add them as she remembered them). This way ensured that by pulling the value of a date-picker field I created for the client, there would be no mistake in the posts order, they would be chronological.

Now this works for MetaBox, that uses the specific rwmb_meta value to pull fields, not the usual get_field that works with ACF. Also, I would not be sure how to adapt the first line to replace the rwmb_versionning_after_save_post and make it work with other custom fields systems.

<?php
add_action( 'rwmb_versionning_after_save_post', 'update_post_title' );

function update_post_title( $post_id ) {
    // Get the field value
    $my_meta = rwmb_meta( 'my_custom_field', '', $post_id );
    
    // Prepare update post
    $my_post = array(
        'ID' => $post_id,
        'post_date' => $my_meta,
    );
    wp_update_post( $my_post );
}
?>

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy