Skip Navigation

[Resolved] I would like to access to a custom field

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a CRED form that creates Post Type A. I have placed this CRED form on Page B. There is a custom field on Page B that I would like to use in my CRED form email notifications. How can I capture the information from the current Page B inside the email notification for Post A?

Solution: Add a generic field to your CRED form. Set the "default" value of this field using the wpv-post-field shortcode, referencing current Page B with the id attribute "$current_page".

[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]

Use the cred_save_data hook to add the value of this generic field to a custom field in Post Type A. Then you can display the value of this custom field in your CRED notifications.

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-fin'], true);

}

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-field

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 2 voices.

Last updated by oriolc 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#552188

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?

#552240

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.

#552275

Happens the same thing,
It appears a data but it is the actual day (24/07/2017), not the custom field data.

#552279

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.

#552398

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.

#552634

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.

#552784

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.

#552787

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.

#553008

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.

#553125

Yes!!!
With the new changes I can obtain the date info.
Thanks a lot, Christian!!!!
I owe you one!!!!!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.