I have noticed a problem with the way dates are filtered in views.
I am trying to sum the values for a field 'potential-value' in the type 'Job'. I want to sum the values within a calendar month. (ie from the 1st of this month to the last day if this month).
However, if I use the filter:
Select posts with custom field:
Start Date is a number between THIS_MONTH(), FUTURE_MONTH(1)
Then it includes the values from the 1st of this month to the 1st of next month. (Ie. if I have a job with the start date of the 1st April then it is added to the sum. This is a problem for me.
However, the exact date is not as important to me as the month, so I thought perhaps I could use cred_save_data, so when the form is saved, if the date is the 1st of the month, then change it to the 2nd of the month. Then at least I would get the right values in the sums, even though the date is slightly off.
I'm not really sure how to change the date like this? Any help would be appreciated.
Tried this, but it's not working so guess I did something wrong?
add_action('cred_before_save_data', 'first_to_second',10,1);
function first_to_second ($form_data)
{
// if a specific form
if ($form_data['id']==140 || $form_data['id']==152)
{
if (isset($_POST['my_custom_field']))
{
$entered_date = $_POST['wpcf-start-date'];
date_modify($entered_date,'+1 day');
$_POST['wpcf-start-date'] = $entered_date;
}
}
}
In my opinion date_modify() does manipulate Weekdays and not Timestamp Dates.
hidden link
Do you agree?
I would rather try this:
strtotime('+1 day', $timestamp);
There are different possibilities but I think that is the best one.
http://stackoverflow.com/questions/2515047/how-do-i-add-24-hours-to-a-unix-timestamp-in-php
hidden link
Please let me know if the above solution works for you, I look forward to your reply!
Thank you for your patience.
Hi Beda
I tried that as follows:
add_action('cred_before_save_data', 'first_to_second',10,1);
function first_to_second ($form_data)
{
// if a specific form
if ($form_data['id']==140 || $form_data['id']==152)
{
if (isset($_POST['my_custom_field']))
{
$entered_date = $_POST['wpcf-start-date'];
strtotime('+1 day', $entered_date);
$_POST['wpcf-start-date'] = $entered_date;
}
}
}
But still no change to the start date. Is there something wrong with the way I'm trying to change it that you can see?
Why do you keep the if (isset($_POST['my_custom_field'])) in your code?
Do you have a field named my_custom_field and need to check if it exists?
The Code on the API page is dummy example code.
Also I am not sure why you use cred_before_save_data
This should be pretty much possible after you saved the data as well.
Let me put a example below how to use it in your case:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
{
//we get stored Date
$entered_date = get_post_meta($post->ID, 'wpcf-start-date', true);
//We add a Day
$new_date = strtotime('+1 day', $entered_date);
// add it back to saved post meta
update_post_meta($post->ID, 'wpcf-start-date', $new_date);
}
}
Working?
Bwahahaha. Can't believe I left the 'my_custom_field' in there.
Anyway I've tried this and still doesn't seem to be changing the date??
add_action('cred_save_data', 'first_to_second',10,2);
function first_to_second($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==140)
{
//we get stored Date
$entered_date = get_post_meta($post->ID, 'wpcf-start-date', true);
//We add a Day
$new_date = strtotime('+1 day', $entered_date);
// add it back to saved post meta
update_post_meta($post->ID, 'wpcf-start-date', $new_date);
}
}
Can you send private login option. Maybe you can see something I can't?
I also see there seems to be a debug popup coming up on the cred form page when I submit, somebody must have left it active. How do I stop this?
I'm so sorry man - don't know what has ridden me,
Of course using the CRED API we access the current edited/created Post with $post_id and not with
$post->ID
So (tested!) just change the code accordingly and it will work:
add_action('cred_save_data', 'first_to_second',10,2);
function first_to_second($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==140)
{
//we get stored Date
$entered_date = get_post_meta($post_id, 'wpcf-start-date', true);
//We add a Day
$new_date = strtotime('+1 day', $entered_date);
// add it back to saved post meta
update_post_meta($post_id, 'wpcf-start-date', $new_date);
}
}
Please let me know if the above solution works for you, I look forward to your reply!
Thank you for your patience.
Thanks Beda
Working perfectly. <does a little dance>