• Resolved Head Goldfish

    (@shoelaced)


    I’m not seeing any other posts about this but it’s happening to me. I have a repeatable date + time metabox and I can enter the date/time and add a row to add another date/time, but when I save it the date/time changes to today’s date and a different time, and any other rows I created go away. It has worked perfectly in the past but no longer. Here’s what I’ve got right now:

    $meta_boxes['event_metabox'] = array(
    		'id'            => 'event_metabox',
    		'title'         => __( 'Event Details', 'cmb2' ),
    		'object_types'  => array( 'event', ),
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true,
    		'fields'        => array(
    
    			array(
    				'name' => __( 'Date(s) & Time(s)', 'cmb2' ),
    				'desc' => __( 'Description', 'cmb2' ),
    				'id'   => $prefix . 'date_time',
    				'type' => 'text_datetime_timestamp',
    				'repeatable' => true,
    			),
    		),
    	);

    On the frontend it shows today’s date and the random time it put in, unless I add another row, in which case it shows both today’s date and 1/1/1970.

    I’ve used the exact same code, literally copy and pasted directly from another project, where it was working great but I’ve just checked and it isn’t working on that site anymore either. Please help! Could it be because of an update?

    https://www.ads-software.com/plugins/cmb2/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Could you pastebin the entire CMB2 code you’re using? It’ll help debug what may be going on.

    Thread Starter Head Goldfish

    (@shoelaced)

    Thanks for the reply, Michael.

    That is the whole code. There are other fields but they’re done the same way.

    This is the complete setup:

    function cmb2_hide_if_no_cats( $field ) {
    	// Don't show this field if not in the cats category
    	if ( ! has_tag( 'cats', $field->object_id ) ) {
    		return false;
    	}
    	return true;
    }
    
    add_filter( 'cmb2_meta_boxes', 'cmb2_event_metaboxes' );
    /**
     * Define the metabox and field configurations.
     *
     * @param  array $meta_boxes
     * @return array
     */
    function cmb2_event_metaboxes( array $meta_boxes ) {
    
    	// Start with an underscore to hide fields from custom fields list
    	$prefix = '_cmb2_';
    
    	/**
    	 * Sample metabox to demonstrate each field type included
    	 */
    	$meta_boxes['event_metabox'] = array(
    		'id'            => 'event_metabox',
    		'title'         => __( 'Event Details', 'cmb2' ),
    		'object_types'  => array( 'event', ), // Post type
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		// 'cmb_styles' => true, // Enqueue the CMB stylesheet on the frontend
    		'fields'        => array(
    
    			array(
    				'name' => __( 'Date(s) & Time(s)', 'cmb2' ),
    				'desc' => __( 'Description', 'cmb2' ),
    				'id'   => $prefix . 'date_time',
    				'type' => 'text_datetime_timestamp',
    				'repeatable' => true,
    			),
    		),
    	);
    // Add other metaboxes as needed
    	return $meta_boxes;
    }
    Thread Starter Head Goldfish

    (@shoelaced)

    I’m looking at the current example-functions.php file and it’s a bit different than mine. I’m wondering if an update messed things up? Do I need to re-do all of my fields? I have a lot of them – this one’s the only one not working right.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The only time you’d need to re-do your fields is if some parameter got changed, and even then, you’d just need to update that one parameter, and not the entire field or set of fields.

    Regarding the issue at hand, I’d need to talk with Justin about it, but it seems the repeatable parameter is causing the issue. I removed that in my testing and it saved the timestamp just fine and accurately.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thread Starter Head Goldfish

    (@shoelaced)

    Hmm, okay, well as I mentioned it used to work great so the error must be from either a plugin update or a wordpress update. In any case, thanks for looking into it. Is there active work being done on the plugin? I have three sites that use this function and one that will be launching soon, and it’d be a huge pain to go add ten independent date pickers or something so that they have the option of multiple event dates, only to change it back when it gets fixed.

    If the error does not happen with the new example-functions code I can try to update mine. I’d love to avoid that but I’ll do it if it’d be a faster solution.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yeah, the plugin is still actively developed, so hopefully we’ll get the issue tracked down and worked out.

    Thread Starter Head Goldfish

    (@shoelaced)

    Hey Michael,

    Any word on how to fix this? We’re supposed to launch in a less than a week and I’ll need to figure out a creative alternative if this won’t be working yet. I noticed that there was a CMB2 update put out yesterday so I updated it but it’s still not working.

    Thanks, hope to hear from you soon! And aside from this issue thanks for the awesome plugin! ??

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I haven’t heard anything about a fix for the issue above, and it remains an open issue, so I assume it’s not fixed yet.

    Is there absolute need for the date field to be a repeatable field? Because as I noted, it worked fine and expected when it wasn’t marked as repeatable.

    Thread Starter Head Goldfish

    (@shoelaced)

    Yes, it needs to be repeatable. I’m using it for an Events feed and many events, such as concerts, will have multiple dates and times.

    My backup plan is to just put in a repeatable text field and have the client type the dates/times, but I’d really love to keep the date formatting that I have in place. Plus again, I’m using this on other websites (where it used to work perfectly), and I would have to change it on all of those websites as well.

    It used to just save all the unix timestamps in an array so I’m using a foreach to format them. It looks like right now the first date gets saved as the current date/time in Greenwich Mean Time, and all of the added repeatable rows save as blank nodes in the array, so they display 1/1/1970.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Keeping an eye on the GitHub issue above would be the best bet at the moment, as I don’t have a hotfix on hand for it. Haven’t had time to try and trace my way through the code to see what’s causing it.

    Thread Starter Head Goldfish

    (@shoelaced)

    Okay so I changed it to four separate non-repeatable fields for now (hoping that they don’t have more than 4 dates per event), but here’s hoping that this can be fixed soon so I can change it back. It’ll work for now but it’s not a very elegant solution. Thanks for your help!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Date will not save – saves today instead.’ is closed to new replies.