• 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

    (@katoen)

    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.