wp_get_object_terms not working in Save_post
-
I am trying to implement Misha’s Publish Posts to Multiple Blogs in WordPress Multisite however the get category function returns uncategorised despite there being assigned categories. After several hours I am completely stumped to what could be causing this.
All help would be greatly appreciated!
add_action( 'save_post', 'misha_post_to_all_sites', 20, 2 ); function misha_post_to_all_sites( $original_post_id, $original_post ){ // do not publish revisions if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $original_post_id; // actually we need only "publish" status if( get_post_status( $original_post ) !== 'publish' ) return $original_post_id; // prevent "Fatal error: Maximum function nesting level reached" remove_action('save_post', __FUNCTION__); // here you have to specify blog IDs where you would like to publish the posts $blog_ids = array( 2, 3 ); // let's get this post data as an array $post_data = array( 'post_author' => $original_post->post_author, 'post_date' => $original_post->post_date, 'post_modified' => $original_post->post_modified, 'post_content' => $original_post->post_content, 'post_title' => $original_post->post_title, 'post_excerpt' => $original_post->post_excerpt, 'post_status' => 'publish', 'post_name' => $original_post->post_name, 'post_type' => $original_post->post_type, ); // terms and post meta as well $post_terms = wp_get_object_terms( $original_post_id, 'category', array('fields' => 'slugs') ); $post_meta = get_post_custom( $original_post_id ); foreach( $blog_ids as $blog_id ) { switch_to_blog( $blog_id ); // if post with the same slug exists, do nothing if( get_posts( array( 'name' => $post_data['post_name'], 'post_type' => $post_data['post_type'], 'post_status' => 'publish' ) ) ) { restore_current_blog(); continue; } $inserted_post_id = wp_insert_post($post_data); wp_set_object_terms( $inserted_post_id, $post_terms, 'category', false); foreach ( $post_meta as $meta_key => $meta_values) { // we do not need these redirects if( $meta_key == '_wp_old_slug' ) { continue; } foreach ($meta_values as $meta_value) { add_post_meta( $inserted_post_id, $key, $meta_value ); } } restore_current_blog(); } }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘wp_get_object_terms not working in Save_post’ is closed to new replies.