Support

Account

Home Forums Backend Issues (wp-admin) Save relationship field after selected Reply To: Save relationship field after selected

  • I use a custom post type “profile” and another “card”. The first allows you to add information about a cosplay (username, first name, date of birth, profile picture, etc..). Thereafter, another custom post type “gallery-cosplay”, allows me to add galleries for each cosplay. You can see an example at: http://shingo-temple.fr/galleries-de-cosplay

    So I wrote two functions. The first allows you to change the title of the gallery with the nickname and the name of the gallery:

     add_action('save_post', 'modify_post_title');
    
    	function modify_post_title($post_id) {
    		
    		 if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
              return;
    		
    		 if ( ! wp_is_post_revision( $post_id ) ) {
    		 		
    		 
    		 if('cosplay-gallery' == get_post_type($post_id) && get_field('card', $post_id)) {
    		 	foreach(get_field('card') as $post_object) {
    				$value = get_field('pseudo', $post_object->ID);
    				$post_title = $value.' : '.get_field('gallery_name', $post_id);
    				
    				
    			}
    			
    		 } 
    	
    	}
    		 
    		 if('acf' != get_post_type($post_id)) {
    		 if ($post_title == '') { $post_title = $_POST['post_title'];}
    		 remove_action('save_post', 'modify_post_title');
    		 $arg = array();
    				$arg['ID'] = $post_id;
    				$arg['post_title'] = $post_title;
    				wp_update_post( $arg );
    				add_action('save_post', 'modify_post_title');
    		 }
    	} 

    The second is to change the permalink:

     function append_slug($data) {
        global $post_id;
        
        if('cosplay-gallery' == get_post_type($post_id) && get_field('card', $post_id)) {
    		foreach(get_field('card') as $post_object) {
    		$value = get_field('pseudo', $post_object->ID);
    		$slug = $value.' : '.get_field('gallery_name', $post_id);
    
    		}
    
    		if ($data['post_name'] != $slug) {
            $data['post_name'] = sanitize_title($slug, $post_ID);
        }
    
       
    } else {
    	if ($data['post_name'] != $data['post_title']) {
            $data['post_name'] = sanitize_title($data['post_title'], $post_ID);
    }
    }
     return $data;
    }
    

    For now, I must save a draft for it to work. Moreover, when I upload images of cosplay, they are renamed “$pseudo.’-‘. $gallery.’-‘.45646.jpg”. If I do not save the draft, it is impossible to have the nickname of cosplay, or the name of the gallery because I use a custom relationship field that points to the profile and custom text field where i write the name of the gallery.

    Ditto for custom post type “card”. This allow gather all the information about a game and share them with other custom post types “review” and “preview”. So I need to retrieve the information in the custom field before they are stored in the database.

    edit: I analyzed the source code of the admin. I could see where the value to retrieve. It remains for me to understand how to use this value with jquery. Thank you!