Skip Navigation

[Resolved] Dynamic post title by CRED form with checkboxes value

This support ticket is created 3 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Purchasing WCER 3 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2139671

I am using this code to create a dynamic post title. All is working great except for grades which returns "Array"

Grades is a checkboxes toolset custom field. Please advise on how to get the individual checkbox values into the title.

//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']==11278) {
$parent_id = $_POST['@school-curricula_parent'];
$parent_title = get_the_title( $parent_id );
$grades = get_post_meta($post_id, 'wpcf-grades', true);
$year = get_post_meta($post_id, 'wpcf-year', true);
$math = get_post_meta($post_id, 'wpcf-math-curriculum', true);
$ela = get_post_meta($post_id, 'wpcf-ela-curriculum', true);
$science = get_post_meta($post_id, 'wpcf-science-curriculum', true);
$title= $parent_title. '-' . $year. '-' . $math.$science.$ela. '-' . $grades;
$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);
}
}

hidden link

#2139975

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To convert the checkboxes array to string we will use the join() function.

Can you please try to use the following code and try to resolve your issue:

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==11278) {
$parent_id = $_POST['@school-curricula_parent'];
$parent_title = get_the_title( $parent_id );
$grades = get_post_meta($post_id, 'wpcf-grades', true);
$grades = join(" - ",$grades);
$year = get_post_meta($post_id, 'wpcf-year', true);
$math = get_post_meta($post_id, 'wpcf-math-curriculum', true);
$ela = get_post_meta($post_id, 'wpcf-ela-curriculum', true);
$science = get_post_meta($post_id, 'wpcf-science-curriculum', true);
$title= $parent_title. '-' . $year. '-' . $math.$science.$ela. '-' . $grades;
$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);
}
}
#2140299

Hi Minesh,

Thank you for the response. Unfortunately that code is still adding "-Array" to the post title.

#2140301

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is really strange.

Can you please send me problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2140925

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've moved the code you added to functions.php to "Custom Code" section offered by Toolset and adjusted the code as given under:
=> 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']==11278) {

$parent_id = $_POST['@school-curricula_parent'];
  
$parent_title = get_the_title( $parent_id );

$grades = get_post_meta($post_id, 'wpcf-grades', true);

    $types_field='grades';
   
   // getting the checkboxes option names and set default option count to 0
   $fields = get_option( 'wpcf-fields');
   $checked_options = array();
    if(isset($fields[$types_field]['data']['options'])){
        foreach ($grades as $k=> $v){
          
          $checked_options[] = $fields[$types_field]['data']['options'][$k]['display_value_selected'];
       	}
      }
  
  
$grades = join(" - ",$checked_options);
 
$year = get_post_meta($post_id, 'wpcf-year', true);

$math = get_post_meta($post_id, 'wpcf-math-curriculum', true);

$ela = get_post_meta($post_id, 'wpcf-ela-curriculum', true);

$science = get_post_meta($post_id, 'wpcf-science-curriculum', true);

$title= $parent_title. '-' . $year. '-' . $math.$science.$ela. '-' . $grades;

$args = array('ID' => $post_id, 'post_title' => $title);

wp_update_post($args);

}

}

Can you please run a test and confirm it works now as expected.

#2141203

That works - thank you!

#2141209

My issue is resolved now. Thank you!