Support

Account

Home Forums General Issues Populate field based on value from related post

Unread

Populate field based on value from related post

  • Hi,

    I have 2 CPT’s of Breeders and cattle sales, and in the cattle sales field group, I have a relational field where they select 1 breeder that is related to that post. The Breeder posts have a text area field for an address, that I would like to use to populate a text area field in a cattle sales post. Is this possible, based on when the post is saved, or when they select the breeder in the relationship field.

    I am really new to php, but by googling and looking at a lot of example on here, I came up with the following code, which I put in the functions.php file, but it doesn’t seem to do anything at all.

    function get_related_breeder_address( $post_id ) {
    	if ($post_id->post_type == 'cattle_sales'){
            	$salebreeder = get_field('breeder_relationship', $post_id );
    		$address = get_field( 'breeder_address', $salebreeder );
    		update_field('sale_breeder_address', $address, $post_id);
    	}  
    	else {
    		return;
    	}
    }
    add_action('acf/save_post', 'get_related_breeder_address');

    I would really appreciate any tips on why my code isn’t working, or if there is a better and easier way to do this.

    Thank you,

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.