Following this ticket: https://toolset.com/forums/topic/i-need-to-implement-a-creed-child-notification/
I would like to access to a meta field (fecha final) of the page where my CRED form is included, in the post created by the CRED form. The page where the form is shown has no parent/child relationship to the post created by CRED.
I've created a generic field:
[cred_generic_field field="fecha-final" type="date" class='' urlparam=""]
{
"required":0,
"validate_format":0,
"default":"[wpv-post-meta id='$current_page' format='meta' meta='fecha-final']" (where current page is another generic field that works!!!!)
}
[/cred_generic_field]
I added fecha final in the custom post that creates the form and finally I added this code to function.php:
add_action('cred_save_data', 'save_parent_fecha_fin',10,2);
function save_parent_fecha_fin($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==550)
{
if (isset($_POST['currentpageid']))
{
$parent_fecha_fin = get_permalink($_POST['currentpageid']);
update_post_meta($post_id, 'wpcf-fecha-final', $_POST['fecha-final'], true);
}
}
}
A date appears, but isn't the good one.
I tried to use get_post_meta in $parent_fecha_fin but nothing happens.
Can you help me?
Hi, try the wpv-post-field shortcode instead of wpv-post-meta. Check the syntax here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-field
[wpv-post-field] will display the raw value from a custom field. Use id="$current_page" like you have in your current shortcode.
Happens the same thing,
It appears a data but it is the actual day (24/07/2017), not the custom field data.
Okay after reviewing the code more closely, I see part of the problem:
if (isset($_POST['currentpageid']))
This code works in the other example but isn't right for your site. Your form uses a different generic field name, "fecha-final":
[cred_generic_field field="fecha-final" type="date" class='' urlparam=""]
I believe you need to change 'currentpageid' in this conditional to 'fecha-final'. Then, test the form. Make sure that the generic date field has the correct default value when the page loads - the date should match the parent post's custom field value. If it does not match, then we need to look more closely at the default value shortcode.
Next, look at this line:
$parent_fecha_fin = get_permalink($_POST['currentpageid']);
I'm not sure what this should be doing, but it does not appear to be used. I think you can remove this line, unless you are planning to use it for something else.
Let me know how it goes.
I made changes that you tell me and now any data appear as a result.
I want to explain two things better:
The custom field that I want to obtain has the name: fecha-final
The custom field for the form, I put the name: fecha-fin
And now I do that changes:
1) I call the generic field fecha-fin
[cred_generic_field field="fecha-fin" type="date" class='' urlparam=""]
{
"required":0,
"validate_format":0,
"default":"[wpv-post-meta id='$current_page' format='meta' meta='fecha-final']"
}
[/cred_generic_field]
2) I eliminate $parent_fecha_fin variable and change isset variable.
add_action('cred_save_data', 'save_parent_fecha_fin',10,2);
function save_parent_fecha_fin($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==550)
{
if (isset($_POST['fecha-fin']))
{
update_post_meta($post_id, 'wpcf-fecha-fin', $_POST['fecha-final'], true);
}
}
After that, no one data appears in the result.
Could be because now we don't have any $post_id? I used $currentpageid to obtain the $post_id where the CRED form is included.
I see a problem with your cred_save_data hook. Look here:
update_post_meta($post_id, 'wpcf-fecha-fin', $_POST['fecha-final'], true);
The new generic field name is 'fecha-fin', so you should use $_POST['fecha-fin'] instead.
Is the generic field showing the correct default date when the form loads? I did not understand your comment about this. If not, you can try hard-coding the parent page ID as a test:
[cred_generic_field field="fecha-fin" type="date" class='' urlparam=""]
{
"required":0,
"validate_format":0,
"default":"[wpv-post-field id='12345' name='fecha-final']"
}
[/cred_generic_field]
Replace 12345 with the ID of the current page. Let me know the results of this test.
I made the two changes:
1) update_post_meta($post_id, 'wpcf-fecha-fin', $_POST['fecha-fin'], true); and it won't change anything. Appears the actual date, 25-07-2017.
I don't want the actual date. I want the date saved in a custom post field named 'fecha-final' which I can find (and see) in the actual page where the cred form is included.
2) I tried your method (hard-code the parent page ID) but the fecha-final date won't appears in fecha-fin date. Just as before, appears the actual date, 25-07-17. Then, it will be that the generic field won't works as we expect.
May I take a look in your wp-admin area to see why this is not working as expected? Private reply fields are enabled here.
Ok I have made a change to your generic field:
[cred_generic_field field="fecha-fin" type="date" class='' urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":"[wpv-post-field id='$current_page' name='wpcf-fecha-final']"
}
[/cred_generic_field]
The 'wpcf-' prefix is required here. This is why the generic field did not include the correct date. Now when the page loads, I can see the fecha-final custom field date value from the current Event page in the value of your generic CRED field:
<input type="text" id="cred_form_550_1-textfield-8-1501072999-147" name="fecha-fin[display-only]" value="25/08/2017"...>
Now your cred_save_data function should receive the correct date information. Please try it again and let me know the results.
Yes!!!
With the new changes I can obtain the date info.
Thanks a lot, Christian!!!!
I owe you one!!!!!