Code Samples: Update Post Title with values from other Fields and Make Post Title Uneditable - Pods Docs

PHOTO EMBED

Mon Nov 27 2023 19:15:54 GMT+0000 (Coordinated Universal Time)

Saved by @dmsearnbit

<?php
​
/* Plugin Name: GNT */
​
/**
 * This filter fills in the post name for our custom post type
 */
add_filter( 'wp_insert_post_data' , 'gnt_modify_post_title' , '99', 2 );
function gnt_modify_post_title($data) {
		
	//only run for our post type
	if ($data['post_type'] == 'accel_feedback') {		
	
		//make a simple date
		$date = date('d-M-Y', strtotime($data['post_date']));
		
		//get user full name or login name
		$user_info = get_userdata($data['post_author']);
		$user_full_name = trim($user_info->first_name ." ". $user_info->last_name);
		if (empty($user_full_name)) {
			$user_full_name = $user_info->user_login;
		}
			
		//create a post title	
		$data['post_title'] =  "$user_full_name - $date";				
		
	}
	
	return $data;
		
}
​
/**
 * This filter updates our read-only pod field to match the real WP title
 */
add_action('pods_api_post_save_pod_item_accel_feedback', 'gnt_post_save_accel_feedback', 10, 3);  
function gnt_post_save_accel_feedback($pieces, $is_new_item, $id) {
	
	//unhook action to avoid infinite loop! :)
	remove_action('pods_api_post_save_pod_item_accel_feedback', 'gnt_post_save_accel_feedback');  
	
	//get the pod and set our read-only 'label' to match the post title
	$pod = pods('accel_feedback', $id);
	$pod->save('label', get_the_title($id));
	
	//reinstate the action
	add_action('pods_api_post_save_pod_item_accel_feedback', 'gnt_post_save_accel_feedback');  			
			
}
content_copyCOPY

https://docs.pods.io/code-snippets/update-post-title-with-values-from-other-fields-and-make-post-title-uneditable/