Skip Navigation

[Resolved] Help me get select custom field “display text” into post title.

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

Problem: I have a custom select field that stores the values 1, 2 and 3 but has different text labels. I would like to display the selected text label using PHP.

Solution:

$type = types_render_field( "analysis-type", array( "id" => $post_id )  );

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 5 years, 9 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 2 replies, has 2 voices.

Last updated by diyanK 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1214145
Screen Shot 2019-03-14 at 12.31.25.png

Hi team!

/This one is going to be a fast and easy one as I already did most of the job./

I am trying to automatically populate the title of a custom post type that is being submitted via CRED form on the front end.

I did a great progress on my own (using the help of previously resolved threads by other users) but found myself stuck.

Here is my code:

add_action('cred_save_data','analysis_title', 10, 2);
function analysis_title($post_id, $form_data) {
// if a specific form
if (($form_data['id']==90) || ($form_data['id']==1123))  {
    $type = get_post_type($post_id, $form_data);
    if ($type == 'analysis') 
    {
        $date= get_post_meta($post_id, 'wpcf-analysis-date', true);
        $date = date("Y-m-d",$date);
        $type = get_post_meta($post_id, 'wpcf-analysis-type', true);
        $key =  $type .'-анализ-' .$date . '-#' . $post_id;
        $slug = sanitize_title($key);
        wp_update_post(array('ID' => $post_id, 'post_title' => $key, 'post_name' => $slug));      
    }
}
}

This results in a title that goes this way "1-анализ-2019-03-03-#126"
where: "1" is the value "custom field content"(see screenshot)
"-анализ-" - just some static text to include
-2019-03-03 - this comes from the "wpcf-analysis-date" field which is reformatted from timestamp to date format.
#126 - is the post ID.

My desired title should read "pH-анализ-2019-03-03-#126",
where "pH" comes from the "display text"(see screenshot) if the user picks the first option.
Or respectively "Химически-анализ-2019-03-03-#126" if the user picks the second option.

#1214326

Hi, I think you should use the types_render_field API instead of get_post_meta:

$type = types_render_field( "analysis-type", array( "id" => $post_id )  );

get_post_meta is the raw value from the database, but the types_render_field API will get the text shown in the select field.
https://toolset.com/documentation/customizing-sites-using-php/functions/#select

#1214376

Sweet! That worked.
I also took a look at the mentioned documentation page but could not understand how to get the missing part right.
Maybe it's a good idea to include this one in the example section too.