scribu/wp-posts-to-posts — Issue #88: use within save_posts action hook
Author: andrewmclagan
Created: 2011-12-15 05:36:23+00:00
State: open

I cannot seem to access connected posts within the save_posts loop.. any ideas?

/**
*	Save api type terms that for this project
*/
function tpf_project_api( $post_id ) {

//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) ) {

// clear term list
wp_set_object_terms( $post_id, '', 'api_category' );

// loop connected posts
$args = array(
'connected_type' => 'contribution_to_project',
'connect_items' => $post_id
);

$loop = new WP_Query( $args );

while ( $loop -> have_posts() ) {

$loop -> the_post();
global $post;

// get terms for this contribution
$terms = wp_get_post_terms( $post->ID, 'api_category', array( 'fields' => 'ids' ));

// add api category term
$terms = array_map('intval', $terms);
$terms = array_unique( $terms );
wp_set_object_terms( $post_id, $terms, 'api_category', true );
}
wp_reset_query();
}

}
add_action( 'save_post', 'tpf_project_api' );