Update CPT Title on Save
-
Hi. I have a Pod called honey_pot with fields rel_student and rewarded_for. The rel_student field is related to a students pod. I have adapted the following Code from the documentation to update the Title on creating a new post. However, it returns the rel_student post id and not the student name. What am I doing wrong? TIA
add_filter( 'pods_api_pre_save_pod_item_honey_pot', 'slug_set_title', 10, 2); function slug_set_title($pieces, $is_new_item) { //check if is new item, if not return $pieces without making any changes if ( ! $is_new_item ) { return $pieces; } //make sure that all three fields are active $fields = array( 'post_title', 'rel_student', 'rewarded_for' ); foreach( $fields as $field ) { if ( ! isset( $pieces[ 'fields_active' ][ $field ] ) ) { array_push ($pieces[ 'fields_active' ], $field ); } } //set variables for fields empty first for saftey's sake $student = $rewarded_for= ''; //This returns post id and not the students name. I need the Students name here if ( isset( $pieces[ 'fields' ][ 'rel_student' ] ) && isset( $pieces[ 'fields'][ 'rel_student' ][ 'value' ] ) && is_string( $pieces[ 'fields' ][ 'rel_student' ][ 'value' ] ) ) { $student = $pieces[ 'fields' ][ 'rel_student' ][ 'value' ]; } //this works without problem if ( isset( $pieces[ 'fields' ][ 'rewarded_for' ] ) && isset( $pieces[ 'fields'][ 'rewarded_for' ][ 'value' ] ) && is_string( $pieces[ 'fields' ][ 'rewarded_for' ][ 'value' ] ) ) { $rewarded_for = $pieces[ 'fields' ][ 'rewarded_for' ][ 'value' ]; } //set post title using $student and $rewarded_for $pieces[ 'object_fields' ][ 'post_title' ][ 'value' ] = $student . ' for ' . $rewarded_for; //return $pieces to save return $pieces; }
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Update CPT Title on Save’ is closed to new replies.