• Resolved faeronsayn

    (@faeronsayn)


    I’d like to know how I could modify post titles depending on the connection they have.

    For example, I am using these connections for TV Shows and episodes.

    So whenever I want to create a post for an episode, I’d like for it to automatically generate the post title of the episode using the TV Show that it is connected to. Since it’ll only connect to one TV Show, it shouldn’t be that hard to know which TV Show title it needs to grab.

    How can I get this to work? I’ve looked into wp_update_post function, and it seems like that may be the way to go, I just need to know how I can retrieve the title of the post that the post that’s being created is connected to. Any help would be great!

    http://wordpress.org/extend/plugins/posts-to-posts/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter faeronsayn

    (@faeronsayn)

    I did some searching and I was able to find a couple of things which maybe related to this issue I am having.

    I tried using the code provided in the documents in my functions.php file

    $connected = new WP_Query( array(
      		'connected_type' => 'posts_to_anime',
      		'connected_items' => get_queried_object(),
      		'nopaging' => true,
    		) );
    
    		$animetitle;
    
    		while ( $connected->have_posts() ) : $connected->the_post();
    
                            $animetitle = get_the_title();
    
    			if (isset($animetitle)) {
    				break;
    			}	
    
    		endwhile;

    This code should grab the first connection it finds and then break out of the loop and subsequently store that connections title into the $animetitle variable. However, I am using this in the following function

    function my_acf_save_post( $post_id )
    {
    	// vars
    	$episode_number = get_field('anime_episode_number' , $post_id );
    
    	if( $episode_number )
    	{
    		remove_action('acf_save_post'   , 'my_acf_save_post'   , 20);
    
    		$connected = new WP_Query( array(
      		'connected_type' => 'posts_to_anime',
      		'connected_items' => get_queried_object(),
      		'nopaging' => true,
    		) );
    
    		$animetitle;
    
    		while ( $connected->have_posts() ) : $connected->the_post();
    
                            $animetitle = get_the_title();
    
    			if (isset($animetitle)) {
    				break;
    			}	
    
    		endwhile;
    
    		$my_post = array();
    		$my_post['ID'  ] = $post_id;
    		$my_post['post_title'] = $animetitle." Online Episode ".$episode_number;
    		$my_post['post_name'] = $animetitle."-online-episode-".$episode_number;
    
    		wp_update_post( $my_post );
    	}
    
    }
    
    // run before ACF saves the $_POST['fields'] data
    add_action('acf_save_post'   , 'my_acf_save_post'   , 20);

    This function uses fields from another addon and couple of static words to set the post title and post slug. I just want to be able to use the connected post title’s name into the post that is being created as you can see by the code above.

    Of course this is giving me an error, which I think is related to the fact that it can’t really figure out what post to check for connected items for, and the query is messing up.

    Here is the error I get, by the way the error only appears if I add the extra posts 2 posts code.

    Warning: Could not find direction(s). in /home2/asdf/public_html/wp-content/plugins/posts-to-posts/core/query-post.php on line 16
    
    Warning: Cannot modify header information - headers already sent by (output started at /home2/asdf/public_html/wp-content/plugins/posts-to-posts/core/query-post.php:16) in /home2/asdf/public_html/wp-includes/pluggable.php on line 876

    I just want to figure out what is called by posts 2 posts or what not when the post is created. That way I could probably use that to grab the post title of the connected post and embed it into the post title of the post that’s being created.

    Thread Starter faeronsayn

    (@faeronsayn)

    Any little direction to what I should be looking at would be great.

    Thread Starter faeronsayn

    (@faeronsayn)

    There was a thread made here: http://wordpress.org/support/topic/plugin-posts-2-posts-using-connected-post-names-to-create-post-title?replies=4

    But the answer was removed for some apparent reason.

    Could anyone at least give me some sort of direction?

    Thread Starter faeronsayn

    (@faeronsayn)

    Could someone tell me what I could use instead to grab the connected post title?

    Thread Starter faeronsayn

    (@faeronsayn)

    It ended up being a lot easier than I expected.

    I used the following code, and the priority didn’t matter, it worked fine.

    $anime_id = p2p_type( 'posts_to_anime' )->get_connected( $post_id )->posts[0]->ID;
    		$animetitle = get_the_title( $anime_id );

    Do you have an example of this working? I think I need to accomplish the same thing, but I’m not exactly sure. Thanks!

    Thread Starter faeronsayn

    (@faeronsayn)

    /*
    add_filter('pre_post_title', 'wpse28021_mask_empty');
    add_filter('pre_post_content', 'wpse28021_mask_empty');
    function wpse28021_mask_empty($value)
    {
        if ( empty($value) ) {
            return ' ';
        }
        return $value;
    }
    
    function my_acf_save_post( $post_id )
    {
    	// vars
    	$episode_number = get_field('anime_episode_number' , $post_id );
    
    	if( $episode_number )
    	{
    		remove_action('acf_save_post'   , 'my_acf_save_post'   , 20);
    
    			$anime_id = p2p_type( 'posts_to_anime' )->get_connected( $post_id )->posts[0]->ID;
    			$animetitle = get_the_title( $anime_id );
    
    		$my_post = array();
    		$my_post['ID'] = $post_id;
    		$my_post['post_title'] = $animetitle." Online Episode ".$episode_number;
    		$my_post['post_name'] = $animetitle."-online-episode-".$episode_number;
    
    		wp_update_post( $my_post );
    	}
    
    }
    
    // run before ACF saves the $_POST['fields'] data
    add_action('acf_save_post'   , 'my_acf_save_post'   , 20);

    Since I am using Advanced Custom Fields, I hook my function into that.

    I just added the lines to grab the ID of the anime, then use it to get the title. Then simply change the post_title and post_name.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Connection Titles embedded into post titles’ is closed to new replies.