Skip Navigation

[Resolved] Date field doesn't save properly

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 14 replies, has 2 voices.

Last updated by Minesh 1 year, 10 months ago.

Assisted by: Minesh.

Author
Posts
#2522079

Dear Sir/Madam,

I have CRED form to update the date field, I have script to query only the date without time to prepare a cron job to make the payment.

Please review the video hidden link

There is a member with id 1116 Amy Leno, the original next bill date is 28-Dec-2022, I change it to today 25-Dec-2022 via the form id 1284, below is the script to query all member whose next-bill-date are today date

    $current_date = date('Y-m-d', strtotime("+8 hours", time()));
		
    $args = array (
        'post_type'  => 'member',
        'post_status' => 'publish',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-uprise-next-bill-date',
                'value' => strtotime($current_date),
                'compare' => '=',
            ),
            array(
                'relation' => 'OR',
                array(
                    array(
                        'key' => 'wpcf-termination-date',
                        'value' => strtotime($current_date),
                        'compare' => '<=',
                    ),
                    array(
                        'key' => 'wpcf-termination-date',
                        'value' => '',
                        'compare' => '=',
                    )
                ),
            )
        )
    );
    $members = get_posts($args);
    print_r($members);
    printf("Payment to be settled today<br/>");
    foreach ( $members as $post ) {
        setup_postdata($post);
        printf("Member: %s (%s)<br>", get_the_title(), get_the_ID());
        wp_reset_postdata();
    }

below is the script to ensure the date is saved with date without time value

function save_data_for_form_with_id_1284($post_id, $form_data)
{
    global $post;
    wp_update_post($post_id, $_POST["wpcf-next-bill-date"], strtotime(date('Y-m-d', $_POST["wpcf-next-bill-date"]['timestamp'])));
    .... (omit other lines)
}
add_action('cred_save_data_1284', 'save_data_for_form_with_id_1284',10,2);

From the video the (0:56) there is no post with next-bill-date as today date, at (1:28), you should find the date has been saved to today date 25-Dec-2022, I edit the member ( post id 1116 ), I click Update button without changing any field value, I execute the script to query again, you should find the result with Amy Leno at video (1:53)

What is the issue why the wp_update_post() does save the date but can't be queried until I click Update to post again?

I provided the admin login credential, you can visit hidden link after you login.

You can visit hidden link to check the today payment record.

#2522745

Was my question first in first serve or last in the first serve? I need support with this question more urgently. Please help.

#2522911

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I see you shared the code as given under with your initial reply:

function save_data_for_form_with_id_1284($post_id, $form_data)
{
    global $post;
    wp_update_post($post_id, $_POST["wpcf-next-bill-date"], strtotime(date('Y-m-d', $_POST["wpcf-next-bill-date"]['timestamp'])));
    .... (omit other lines)
}
add_action('cred_save_data_1284', 'save_data_for_form_with_id_1284',10,2);

With the above code I see the following line could be the issue:

wp_update_post($post_id, $_POST["wpcf-next-bill-date"], strtotime(date('Y-m-d', $_POST["wpcf-next-bill-date"]['timestamp'])));

- I see you are using the $_POST["wpcf-next-bill-date"] as the custom field name - does that contains the custom field name?

Normally, its should be like as follows - Can you please change the above line as given under:

wp_update_post($post_id, 'wpcf-next-bill-date', strtotime(date('Y-m-d', $_POST["wpcf-next-bill-date"]['timestamp'])));

Does that helps or I still need to take a closer look?

#2522967

Dear Minesh,

Sorry I rewrite the code for easy reading. The original code should be

    foreach ($fields as $field) {
    	if ($_POST["wpcf-{$field}"] <> $_POST["original-{$field}"]) {
				if (in_array($field, array("uprise-next-bill-date", "termination-date"))) {
					if ($_POST["wpcf-{$field}"]['timestamp'] <> $_POST["original-{$field}"]) {
						$isChange = true;
						$args = [
							'ID' => $post_id,
							'post_date' => date( 'd-m-Y H:i:s', strtotime('now') ),
							'post_date_gmt' => gmdate( 'd-m-Y H:i:s', strtotime('now') ),
							$_POST["wpcf-{$field}"] => strtotime(date('d-m-Y', $_POST["wpcf-{$field}"]['timestamp']))
						];
						wp_update_post($args);
						$post_content .= sprintf("Change %s value from %s to %s\n", $field, date('d-m-Y', $_POST["original-{$field}"]), date('d-m-Y', $_POST["wpcf-{$field}"]['timestamp']));
					}
				} else {
    			$isChange = true;
    			$post_content .= sprintf("Change %s value from %s to %s\n", $field, $_POST["original-{$field}"], $_POST["wpcf-{$field}"]);
    		}
    	}
    }

When I load the form, I store all fields' values with original-XXXX; when saving the form, I compare all fields' values before and after. If the value is changed, I create a log to record what values were changed

Only the uprise-next-bill-date and termination-date are in date format. Did you watch the video? The value was saved, but I don't know why it can't be queried; if I save a post from the dashboard, it can be quired.

#2522971

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I suggest you modify the range you are using to query the date field.

For example:
- you will require to query the date field with numeric type. I've adjusted the code as given under:

$args = array (
    'post_type'  => 'member',
    'post_status' => 'publish',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'wpcf-uprise-next-bill-date',
            'value' => strtotime($current_date),
            'type' => 'numeric',
             'compare' => '>=',
        ),
        array(
            'relation' => 'OR',
            array(
                array(
                    'key' => 'wpcf-termination-date',
                    'value' => strtotime($current_date),
                   'type' => 'numeric',
                    'compare' => '<=',
                ),
                array(
                    'key' => 'wpcf-termination-date',
                    'value' => '',
                    'compare' => '=',
                )
            ),
        )
    )
);

Dose the above helps?

Also, with the following argument:

array(
                    'key' => 'wpcf-termination-date',
                    'value' => '',
                    'compare' => '=',
                )

Do you want to query the posts where "termination-date" is not set yet? if yes: then you will have to use the clause "NOT EXISTS" or "EXISTS" as per your requirement and accordingly you can build your query argument.
please check the following related ticket:
- https://toolset.com/forums/topic/filter-on-empty-or-null-or-not-exist/

#2523155
Screenshot 2022-12-27 at 10.31.47 PM.png
Screenshot 2022-12-27 at 10.29.58 PM.png
Screenshot 2022-12-27 at 10.29.05 PM.png

Dear Minesh,

I tried your code, but it did not work. Please refer to the screenshot. I captured the database table before and after the update, I found the meta 'wpcf-termination-date' was lost if I saved it from the form. Once I updated the post from dashboard, the meta existed without value.

I try adding the NOT EXISTS as below but still not work.

$args = array (
    'post_type'  => 'member',
    'post_status' => 'publish',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'wpcf-uprise-next-bill-date',
            'value' => strtotime($current_date),
            'type' => 'numeric',
             'compare' => '=',
        ),
        array(
            'relation' => 'OR',
            array(
                array(
                    'key' => 'wpcf-termination-date',
                    'value' => strtotime($current_date),
                   'type' => 'numeric',
                    'compare' => '<=',
                ),
                array(
                    'key' => 'wpcf-termination-date',
                    'compare' => 'NOT EXISTS'
                )
            ),
        )
    )
);

Once I update the post from dashboard then I can query it out. Now I will have two possible cases, could you advise how the $args should be

1. I save the post from the form ( meta 'wpcf-termination-date' will be lost )

2. I update the post from dashboard ( meta 'wpcf-termination-date' will be added without value )

#2523225

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to know why you want to query termination date as termination date is either not available or if its available then its empty.

That is why I shared before:
=> https://toolset.com/forums/topic/date-field-doesnt-save-properly/#post-2522971

Do you want to query the posts where "termination-date" is not set yet? if yes: then you will have to use the clause "NOT EXISTS" or "EXISTS" as per your requirement and accordingly you can build your query argument.

#2523263

Dear Minesh,

If you refer to my three screenshots before and after I save them from the form and update them from the dashboard, you should find the termination date is existed and is empty in value if I update the post from the dashboard

The termination date has no value by default; the WP save the meta_key without value, but if I do wp_update_post(), the termination data meta_key is removed from the database.

I have the below three conditions to query the correct post.

Condition 1: (I update the next bill date as the current date from the CRED form; leave termination-date empty)
next-bill-date = current date
termination-date is NOT EXISTS

        array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-uprise-next-bill-date',
                'value' => strtotime($current_date),
                'type' => 'numeric',
                 'compare' => '=',
            ),
            array(
                'key' => 'wpcf-termination-date',
                'compare' => 'NOT EXISTS'
            ),
        ),

Condition 2: (I update the next bill date as the current date from the CRED form, termination-date is later than the current date)
next-bill-date = current date
termination-date >= current date

        array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-uprise-next-bill-date',
                'value' => strtotime($current_date),
                'type' => 'numeric',
                 'compare' => '=',
            ),
            array(
                'key' => 'wpcf-termination-date',
                'value' => strtotime($current_date),
                'type' => 'numeric',
                'compare' => '>=',
            ),
        ),

Condition 3: (I update the next bill date as the current date from the CRED form, termination-date is empty but I update the post from dashboard)
next-bill-date = current date
termination-date is EXISTS or value is ''

    'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-uprise-next-bill-date',
                'value' => strtotime($current_date),
                'type' => 'numeric',
                 'compare' => '=',
            ),
            array(
                'key' => 'wpcf-termination-date',
                'value' => '',
                'type' => 'numeric',
                'compare' => '='
            ),
        ),

How can I put all three conditions in one meta_query together?

#2524399

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If you refer to my three screenshots before and after I save them from the form and update them from the dashboard, you should find the termination date is existed and is empty in value if I update the post from the dashboard

The termination date has no value by default; the WP save the meta_key without value, but if I do wp_update_post(), the termination data meta_key is removed from the database.
==>
What I would like to know that do you think that you will have to take termination date in order to query the results as termination date is either not available or if its available its empty. Is there a case that termination date will have value?

#2524491

Dear Minesh,

Termination date will be one of the condition I need to query if there is value, the problem is if post be updated from dashboard, the termination date is EXISTS without value by default but this meta_key will be removed if it is saved via function save_data_for_form_with_id_1284()

I can't limit administrative staff to have save post from dashboard because they have the access.

#2524751

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Lets go step by step:

First lets check if you get the correct next bill date results.

Can you please try to use the following code and check:

$current_date = date('Y-m-d', strtotime("+8 hours", time()));
$end_time = strtotime('+1 day', $current_date ) - 1;

$args = array (
    'post_type'  => 'member',
    'post_status' => 'publish',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'wpcf-uprise-next-bill-date',
            'value' => array($current_date, $end_time), 
            'type' => 'numeric',
             'compare' => 'BETWEEN',
        ),
     )
);

Do you get the correct post that is set for todays date.

#2526275

Dear Minesh,

Basically, I don't have a problem with querying the wpcf-uprise-next-bill-date. I solve the problem with the below code but just because I have to have a complicated condition to handle the empty termination-date due to different to the standard post update from dashboard and save_data_for_form_with_id_1406(), you can see my comment in code and know how I handle the condition. Is there any way to make it a bit simple?

				$args = array (
				    'post_type'  => 'member',
				    'post_status' => 'publish',
				    'meta_query' => array(
				        'relation' => 'AND',
				
				// query wpcf-uprise-next-bill-date must be today date
				        array(
				            'key' => 'wpcf-uprise-next-bill-date',
				            'value' => strtotime($current_date),
				            'type' => 'numeric',
				             'compare' => '=',
				        ),
				        
				        array(
				            'relation' => 'OR',
				            
				            array(
				                'relation' => 'OR',
				                
				                array(
				                    'relation' => 'AND',
				
				                    
				                    array(                                          
				                        'key' => 'wpcf-termination-date',
				                        'value' => '',
				                        'type' => 'numeric',
				                        'compare' => '='
				                    ),                                              // query if termniation-date is empty                                              
				
				                    array(
				                        'key' => 'wpcf-termination-date',
				                        'compare' => 'EXISTS'
				                    ),                                              // query if terminsation-date is existed
				                ),                                                  // query if post updated from dashboard without termniation-date value
				                
				                
				                array(                                              // query if post updated from CRED form without termniation-date value
				                    'key' => 'wpcf-termination-date',
				                    'compare' => 'NOT EXISTS'
				                ),                                                  // query if termination-date is not existed
				            ), 
				            
				            
				            array(                                                  // query if wpcf-termination-date has value later than today 
				                'key' => 'wpcf-termination-date',
				                'value' => strtotime($current_date),
				                'type' => 'numeric',
				                'compare' => '>=',
				            ),
				        )
				    ),
				);
				
        $members = get_posts($args);
#2526291

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure why you are using the equal to comparison with the date field as date field stored as timestamp rather using date range but glad to know that you found the way how you wanted to make it work. There is no other way.

#2526697

It is because all dates be stored in date('Y-m-d 00:00:00') format. It is not the query issue to the next-bill-date.

Or is it possible to do the save_data_for_form_with_id_1406() the same as the post update from the dashboard? I mean the handling of the termination date.

#2526879

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you handle the termination date field value with the hook "save_data_for_form_with_id_1406".

You can add update_post_meta() and set the termination date value either to empty or as required - does that helps?