Skip Navigation

[Resolved] cred custom field in post title

This support ticket is created 4 years, 6 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 15 replies, has 2 voices.

Last updated by David Gimenez 4 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#1675229

Hi there.

I want to create a custom post title from 2 custom fields in a cred.

It works fine but partially.

The function is

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if (($form_data['id']==291) or ($form_data['id']==265)) {
		$name = get_post_meta($post_id, 'wpcf-nom-de-la-seccio-del-menu', true);
		$menuX = get_post_meta($post_id, 'wpcf@seccio-del-menu.parent', true);
        $title= $name. ' - ' . $menuX;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

The custom field is @seccio-del-menu.parent. How can I get the value of this field?

Thanks in advanced.

#1675319

Hello and thank you for contacting the Toolset support.

You are trying to pull a custom field from a related post, which is wrong. You will need to get the related post first, then use it's ID to get the custom field. Change this line:

$menuX = get_post_meta($post_id, 'wpcf@seccio-del-menu.parent', true);

With:

$related = toolset_get_related_post( $post_id, 'seccio-del-menu' );
$menuX = get_post_meta($related->ID, 'custom-post-type-slug-from-the-related-post', true);

It uses the toolset_get_related_post function from the Toolset relationship API, more reading here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

If this does not help, I'll need to take a closer look at your admin area, the post type, the relationships, and the form.

#1675391

Hallo again Jamal.

Thanks for your answer

It doesn't work.

seccio-del-menu is a repeteable field of post type menu. Perhaps here is the problem...

#1676603

Thank you for your feedback.

A repeatable group field is, under the ground, a custom post type with a hidden one-to-many relationship to the post type where it is used. It can't be accessed directly with get_post_meta. You will need to get the post, then get its meta data. Please check if the following code works for you:

$related = toolset_get_related_post( $post_id, 'seccio-del-menu' );
$menuX = get_post_meta($related->ID, 'seccio-del-menu, true);

If it does not, allow me temporary access(WordPres+FTP) to check this closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1676913

Thank you for the access credentials. Unfortunately, I was not able to test the code, the first field does not return anything hidden link

Please give it a try and if it does not work, I'll need an example to test with(what info to enter in each field and what is the expected result).

The following code should work:

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if (($form_data['id']==291) or ($form_data['id']==265)) {
		$name = get_post_meta($post_id, 'wpcf-nom-de-la-seccio-del-menu', true);
		$related = toolset_get_related_posts( $post_id, 'seccio-del-menu', array('query_by_role' => 'parent') );
		if ( count( $related ) > 0 ) {
			$related = $related[0];
		}
		$menuX = get_post_meta($related, 'wpcf-nom-de-la-seccio-del-menu', true);
        $title= $name. ' - ' . $menuX;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}
#1676975

Hi Jamal.

First of all... thanks for your help.

The code doesn't work.

I created a business and a menu with your user. Now you can test it. You can create or modify seccion del menu. It doesn't add menuX to the post title.

Thanks in advanced

#1677761

I had to install the File Manager plugin in order to activate PHP debugging and check why is the code not working.

The code will not work for the edit form, with id 272. Line 3 needs to change to:

    if (($form_data['id']==291) or ($form_data['id']==265) or ($form_data['id']==272)) {

I also could not find the field "wpcf-nom-de-la-seccio-del-menu" in line 4. The $name will be empty. Because of that, my menu name is now "– ENtrants", the name was successfully pulled from the child post, but the name is always empty.

Can you explain a bit what do you expect to have in $name?

#1678041
Captura de pantalla 2020-06-26 a las 16.25.29.png
Captura de pantalla 2020-06-26 a las 16.18.24.png

Hi Jamal. First of all... thanks for your help

To create a seccion of menu i have a form with fields:

Menu (@seccio-del-menu.parent)
Nom de la seccio del menu: wpcf-nom-de-la-seccio-del-menu
Position: wpcf-posicio-de-la-seccio-del-menu
Contingut: contingut-de-la-seccio-del-menu

If you to the wp-admin and edit, by example, menu ideal, you'll see that a repeteable fiels called seccio del menu
Post title is Pizzes - I want to add afetr - the name of the menu ((@seccio-del-menu.parent).
After post title I have Nom de la secio del menu -> wpcf-nom-de-la-seccio-del-menu

The funcion is done to get nom de la seccio del menu + nom del menu and create a tring with that 2 fields to insert it like as post title of repeteable field seccio del menu. I attach 2 screenshots.

So.. this is the reason that i want $title= $name. ' - ' . $menuX

I need to get the data of name of menu of edit and create seccio del menu, and do a new string of nom de la seccio del menu + nom del menu and insert in post title of repetable field seccio del menu

In post title of repeteable field $menuX is missing

Do you understand now my problem? It's impossible to get the name of menu in dropdown in the form.

#1678097

I am really sorry, but I still do not understand. Here what I understood before:
- We are seeking to update the name of the menu with this rule ($title= $name. ' - ' . $menuX)
- $menuX is the custom field "nom-de-la-seccio-del-menu" from the child in the repeatable group "seccio-del-menu"
- $name: This I do not yet know where it should come from?

Now I understand that you want to update the name of the children's posts in the repeatable group by adding "nom-de-la-seccio-del-menu", right?

What are the results that you would expect for the "Menu Ideal" screenshot (Captura de pantalla 2020-06-26 a las 16.25.29.png)?
Do you expect to have the child post in the repeatable group to have title "Pizzes - Menu Ideal"?
Do you expect to change the title "Menu Ideal" to something? What title?

#1678233
Captura de pantalla 2020-06-26 a las 18.55.27.png

Hi again Jamal.

I add an screenshot.

I want to get an string with nom de la secció del menu (apertitus de la casa = wpcf-nom-de-la-seccio-del-menu) + name of the post title parent (menu espanyol = @seccio-del-menu.parent) + -> Result: aperitius de la casa - menu espanyol where the pink squared of the screenshot

I hope I have explained myself better

Thanks for your time

#1678877

Thank you! That's clear.

I tried the following code but it did not work on your website and PHP debugging is not configured and I was not able to configure it, in order to debug this piece of code. So I took a Duplicator copy of your website for local debugging. I'll get back to you as soon as possible.

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if (($form_data['id']==291) or ($form_data['id']==265) or ($form_data['id']==272)) {
        $post = get_post($post_id);
		$related = toolset_get_related_posts( $post_id, 'seccio-del-menu', array('query_by_role' => 'parent') );
        if ( count( $related ) > 0 ) {
            $related = $related[0];
        }
        $menuX = get_post_meta($related, 'wpcf-nom-de-la-seccio-del-menu', true);
        $title= $menuX. ' - ' . $post->post_title;
        $args = array('ID' => $related, 'post_title' => $title);
        wp_update_post($args);
    }
}

Thank you for your patience.

#1679067

Hi Jamal.

wp-debug is working.

Let me know if you need anything.

Thanks!

#1682265

My apologies for the late reply, but I do not work on Sundays and Mondays.

Please check the following code, it seems to work for me. See this screenshot hidden link

//Create a dynamic post title by the CRED form.
add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if (($form_data['id']==291) or ($form_data['id']==265) or ($form_data['id']==272)) {
        $post = get_post($post_id);
        $related = toolset_get_related_posts( $post_id, 'seccio-del-menu', array('query_by_role' => 'parent') );
        if ( count( $related ) > 0 ) {
            foreach ($related as $menuX) {
                $nom = get_post_meta( $menuX, 'wpcf-nom-de-la-seccio-del-menu', true );
                $title = $nom . ' - ' . $post->post_title;
                $args = array( 'ID' => $menuX, 'post_title' => $title );
                wp_update_post( $args );
            }
        }
    }
}
#1683613

Hi Jamal.

FIrst of all.... Thnaks for your time and your help!

It's not working form me! Perhaps is it cause by php version. I don't understand why it doesn't work. Now don't save anything. I get a CRED Auto Draft 1dde5a2f9b30f66cba28536f0974ba68.

Is the function correct? Must i insert anything in toolset settings?

Thanks in advanced

#1684123

I don't see why is this working on my local copy and not on your website. Then I realized that the form in my local copy is with ID 272 and the form on your website is with ID 291.

The code that I shared assumes that it will be executed for forms about "Menus" custom post type. But the form "291" is for a different custom post type "Seccio del menu". So, it won't work for that.

The code will only work for Menus forms(especially edit forms), as it needs the menu to have the child posts in the repeatable group. The function toolset_get_related_posts is used to get the child posts in the repeatable field "seccio-del-menu". Then we go through each one with the foreach closure and we build a new title for the child in the repeatable group.

If you want to hook into the form of "seccio-del-menu", you will have to change the toolset_get_related_posts call and look by role "child":

        $related = toolset_get_related_posts( $post_id, 'seccio-del-menu', array('query_by_role' => 'child') );

You won't need to use foreach, as you will get only one parent. Then you can build the title and save it.

Please consider that this is custom code. And is out of the scope of the support forum, If you are not comfortable with coding, consider hiring a developer or one of our partners https://toolset.com/contractors/

If one of our API(such as the "toolset_get_related_posts" function) is not returning what's expected, open a new ticket and we'll check why is that.