• Resolved tnightingale

    (@tnightingale)


    Is there a filter to add another date format to the options for the datepicker output? I need the ‘Ymd’ format – to match the ACF format for storing dates in the DB. I’m using a Forminator form to create posts with a custom date field and sorting the posts by that field. The formats have to all be the same for the sort to work. If a post is edited later and a new date is saved, it switches to the ACF format.

    Thanks in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @tnightingale

    I hope you’re well today!

    There’s a custom code that might help here. If you want to give it a try, you’ll find it here:

    https://gist.github.com/wpmudev-sls/7fcf5ad05a95d7f664cdf52a27f7b530

    If you need to adjust date format, it can be done in this line of the code:

    $meta_field['value'] = $date->format('Y-m-d');

    Best regards,
    Adam

    Thread Starter tnightingale

    (@tnightingale)

    Thank you! In the meantime I found another solution – based on a forum topic where someone was asking about setting the post title from a custom field value. I adapted it and it works fine with much less code. In functions.php. It’s form-specific:

    add_action( 'forminator_post_data_field_post_saved', function( $post_id, $field, $data ){
    	if( $data['form_id'] == 750 ){ // race form ID 
    		$racedate = date('Ymd', strtotime($_POST['date-1']));
    
    		$my_race = array(
    			'ID'          => $post_id,
    			'race_date'   => $racedate,
    		);
    
    		// Update the post into the database
    		wp_update_post( $my_race );
    	}
    }, 10, 1 );
    
    Thread Starter tnightingale

    (@tnightingale)

    Darn, spoke too soon. That isn’t working – produces a generic error message and the form won’t submit. Do you know why? I would love to get that working as I also need to be able to write a calculated title to the post as per the original source of that snippet.

    In the meantime I’ll try the custom plugin you provided above, to fix the date issue at least.

    Thread Starter tnightingale

    (@tnightingale)

    Update – I have tested the following:

    1) The mu-plugin you linked to above. Without the if ( ! defined( 'ABSPATH' ) ) { section at the top as that’s already part of the wp-config.php file as per the discussion here: https://stackoverflow.com/questions/43212340/what-is-meant-by-if-defined-abspath. When I tried it at first, the form wouldn’t display at all. Probably related to the AJAX display.

    Result: Error message after submitting form instead of success message. No details of the error. But post has been created and confirmation sent. However the sorting by date is still incorrect.

    2) A fixed version of the action I tried first, using correct # of arguments:

    add_action( 'forminator_post_data_field_post_saved', 'mg2022_fm_fix_compatible_on_date_meta_with_acf', 10, 3 );
    function mg2022_fm_fix_compatible_on_date_meta_with_acf( $post_id, $field, $data ){
    	if( $data['form_id'] == 750 ){ // race form ID 
    		$racedate = date('Ymd', strtotime($_POST['date-1']));
    
    		$my_race = array(
    			'ID'          => $post_id,
    			'race_date'   => $racedate,
    		);
    
    	// Update the post into the database
    		wp_update_post( $my_race );
    	}
    }
    

    Result: form submits without errors, but sorting is still incorrect.

    3) using add_filter instead of add_action but otherwise same as (2).

    Result: same as (2).

    What else can I try?

    • This reply was modified 3 years, 10 months ago by tnightingale.
    Thread Starter tnightingale

    (@tnightingale)

    OK I figured it out. Testing for form_id doesn’t work on this action, that was from a different one you helped me with related to updating the submission and email data.
    For this one all I had to do was check for post_type via the $post_id – now it’s working! I also had to update the wp_update_post() arguments to properly insert the meta field value.

    function mg2022_fm_fix_compatible_on_date_meta_with_acf( $post_id, $field, $data ){
    	if( get_post_type($post_id) == 'race'){ // only do this for race submissions
    
    		$racedate = $_POST['date-1'];
    		$racedatef = date('Ymd', strtotime($racedate));
    
    		$my_race = array(
    			'ID'          => $post_id,
    			'meta_input'  => array( 'race_date' => $racedatef ),
    		);
    
    	// Update the post into the database
    		wp_update_post( $my_race );
    	}
    }
    add_action( 'forminator_post_data_field_post_saved', 'mg2022_fm_fix_compatible_on_date_meta_with_acf', 10, 3 );
    • This reply was modified 3 years, 10 months ago by tnightingale.
    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @tnightingale,

    We are happy to hear that the issue has been resolved.
    Please let us know if you need further help.

    Kind regards,
    Zafer

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Add date format Ymd (no separators)’ is closed to new replies.