WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Connection Titles embedded into post titles (8 posts)

  1. faeronsayn
    Member
    Posted 1 year ago #

    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/

  2. faeronsayn
    Member
    Posted 1 year ago #

    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.

  3. faeronsayn
    Member
    Posted 1 year ago #

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

  4. faeronsayn
    Member
    Posted 1 year ago #

    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?

  5. faeronsayn
    Member
    Posted 1 year ago #

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

  6. faeronsayn
    Member
    Posted 1 year ago #

    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 );
  7. NWTD
    Member
    Posted 1 year ago #

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

  8. faeronsayn
    Member
    Posted 1 year ago #

    /*
    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic