Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a default title for all the posts that are created, the title is generated by a couple of things, such as the post fields.

I was able to write code for getting the post fields into the title, but I can't seem to figure a way out to get the posts-to-posts connected post's title into the title of the post that's being created. I tried to add the following code into a function that I already created..

$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 was embedded into the following function, which works perfectly if the above code isn't present..

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);

Now with the connected posts code added I get the following error

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.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.