Title: [Plugin: Posts 2 Posts] Add a connected post to title when saving
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Add a connected post to title when saving

 *  [katoen](https://wordpress.org/support/users/katoen/)
 * (@katoen)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-add-a-connected-post-to-title-when-saving/)
 * What I would like to achieve:
    Add custom post type ‘Game’: Testing Game Connect
   to custom post type ‘Team’: A Team
 * When saving/publishing: change title of the ‘Game’ to: Testing Game – A Team
 * I’m using this code in functions.php
 *     ```
       function change_title($data, $postarr) {
   
       	if ($data['post_type'] == 'games') {
   
       	$data['post_title'] = 'new title';
       	return $data;
       	} else {
       	return $data;}
       }
   
       add_filter('wp_insert_post_data','change_title',99,2);
       ```
   
 * I looked through the code, but can’t find how the connection is saved when publishing
   the post (but i’m not very php savvy…).
    Any hints on how I could achieve this?

Viewing 1 replies (of 1 total)

 *  Thread Starter [katoen](https://wordpress.org/support/users/katoen/)
 * (@katoen)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-add-a-connected-post-to-title-when-saving/#post-2271031)
 * Ok, got this working, but maybe not best way to do it…
 *     ```
       function custom_post_type_title ( $post_id ) {
           global $wpdb;
           if ( get_post_type( $post_id ) == 'games' ) {
   
       	$connected = p2p_get_connected ($post_id, 'to');
       	foreach ($connected  as $key => $value) {
       		if ( get_post_type( $value ) == 'team' ) {
               $title = get_the_title($value);
       break;
       		}
       	}
               $where = array( 'ID' => $post_id );
               $wpdb->update( $wpdb->posts, array( 'post_title' => $title ), $where );
           }
       }
       add_action( 'save_post', 'custom_post_type_title',100,2);
       ```
   
 * I tried replacing the p2p_get_connected with
 *     ```
       $connected1 = get_posts( array(
               'post_type' => 'team',
               'connected' => $post_id,
               'posts_per_page' => 1,
               'suppress_filters' => false
           ) );
       ```
   
 * but this didn’t work (empty array)

Viewing 1 replies (of 1 total)

The topic ‘[Plugin: Posts 2 Posts] Add a connected post to title when saving’ is
closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/posts-to-posts_7a8e9d.svg)
 * [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/posts-to-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/posts-to-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/posts-to-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/posts-to-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/posts-to-posts/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [katoen](https://wordpress.org/support/users/katoen/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-add-a-connected-post-to-title-when-saving/#post-2271031)
 * Status: not resolved