Date timestamp not saving for clonable group when saved with rwmb_set_meta

Support MB Group Date timestamp not saving for clonable group when saved with rwmb_set_meta

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #47696
    hannuhhannuh
    Participant

    Hi!

    I have clonable group defined:

    'fields'   => array(
    array(
    							'id'     => 'recurring_event_group',
    							'type'   => 'group',
    							'clone'  => true,
    							'collapsible' => false,
    							'add_button' => 'Add more',
    							'group_title' => 'Times',
    							'save_state' => true,
    							'sort_clone' => false,
    							'fields' => array(
    			            array(
    			                'id'   => 'event_date',
    			                'name' => 'Date start',
    			                'desc' => '',
    											'type'    => 'date',
    									    // Date picker options. See here http://api.jqueryui.com/datepicker
    									    'js_options' => array(
    									        'dateFormat'      => 'dd-mm-yy',
    									        'showButtonPanel' => false,
    									    ),
    									    // Save value as timestamp
    									    'timestamp' => true,
    											'required' => true,
    			            ),

    I programmatically adding to the group values right after creating a new post with wp_insert_post:

    
    $recurring_events[] = array(
    'event_date' => strtotime($start_date),
    ...
    
    rwmb_set_meta( $new_post_id, 'recurring_event_group', $recurring_events );

    When then viewing the newly created post on admin side the date field has correctly the date. But when viewing the frontend the date is shown as 1.1.1970.

    If then clicking Save from the (Gutenberg) admin the frontend shows the date correctly after the save.

    What causes this that the date timestamp value is not saved even though it is shown correctly on admin side in the date field? Is there a way to fix this so that there is not need to save the post from admin side?

    Thanks!

    #47704
    PeterPeter
    Moderator

    Hello,

    Do you hook your callback function to the init hook with a priority later than 20 so the function rwmb_set_meta() can work? It is noted in the documentation
    https://docs.metabox.io/functions/rwmb-set-meta/

    This code works as well on my site to set the date field value

    add_action( 'init', function() {
        $start_date = '20-04-2025';
        $recurring_events[] = array(
            'event_date' => strtotime($start_date),
        );
    
        rwmb_set_meta( 198, 'recurring_event_group', $recurring_events );
    }, 99 );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.