• Resolved pearler

    (@pearler)


    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)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @pearler

    We always store the relation ID, not the title.
    If you need to get the title you can use functions like get_posts() to fetch the full related object and use it’s data.

    Cheers, Jory

    Thread Starter pearler

    (@pearler)

    Thanks Joey. My code is to update the title on save. I’m not sure how I would use get_post function to update the title when the post hasn’t been created.

    Thread Starter pearler

    (@pearler)

    Sorry Jory. Auto correct

    Thread Starter pearler

    (@pearler)

    Never mind Jory, I got it to work by using a fetch from the students pod using the id that was returned. Thanks for your help.

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @pearler

    Good to hear you’ve solved it. Good luck with your project!

    Cheers, Jory

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Update CPT Title on Save’ is closed to new replies.