I'm trying to make a centralized Posts delivery system for a WPMU-Installation.
The Setup:
- Latest Wordpress Installation
- Plugin: Advanced Custom fields (ACF)
- Plugin: WPML
The Goal:
I want to create a post, which should finally be delivered to the WPMU installations, which I have chosen before.
The Problem 1:
I'm not able to correctly save the post thumbnail to the new created post.
The Problem 2:
As I already mentioned I have several installations in my WPMU. So there are also tons of ACF Fields with different field keys. Do anyone know if there is an easy way to get the field values from the post, into the newly created posts?
In ACF-Forums they wrote, that I should use the field key to handle this. But I don't want to manually go through each site, language to get the field keys.
The Code so far:
/* Pushing News Posts to previous selected hotels */
function push_posts_to_hotels( $post_id, $post, $update ) {
$slug = 'post';
$error = false;
if ( $slug != $post->post_type ) return;
if(get_field('news_events_type',$post_id) && get_field('news_events_type',$post_id) == 1) {
if(get_field('send_to_hotels',$post_id) && count(get_field('send_to_hotels',$post_id)) != 0) {
global $wpdb;
$post_meta_infos = $wpdb->get_results( "SELECT * FROM kas_3_postmeta WHERE post_id = $post_id", OBJECT );
//pre($post_meta_infos); die;
//Switching Blogs
$all_blogs = wp_get_sites();
$original_blog_id = get_current_blog_id();
//Send posts to blogs
$send_to = get_field('send_to_hotels',$post_id);
//Setting data
$post = get_post( $post_id );
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'publish',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
$thumb = "";
if(get_post_thumbnail_id($post_id)) {
$thumb_id = get_post_thumbnail_id($post_id);
$thumb = wp_get_attachment_url($thumb_id);
}
//pre($thumb);
//die;
foreach($all_blogs as $blog) {
switch_to_blog($blog['blog_id']);
if(in_array($blog['blog_id'],$send_to)) {
global $wpdb;
$new_post_id = wp_insert_post( $args , true);
if(!empty($thumb)){
$wp_filetype = wp_check_filetype(basename($thumb), null );
$getImageFile = $thumb;
$attach_id = wp_insert_attachment( $args, $getImageFile, $new_post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $getImageFile );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $new_post_id, $attach_id );
}
/*$field_key = "";
update_field($field_key, get_field('slideshow_header',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_headline',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_subtitle',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_pretext',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_maintext',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_only_listCheckbox',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_no_link',$post_id) ,$new_post_id);
update_field($field_key, get_field('news_events_type',$post_id) ,$new_post_id);
update_field($field_key, get_field('news_events_event_date',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_gallery',$post_id) ,$new_post_id);
update_field($field_key, get_field('content_downloads',$post_id) ,$new_post_id);
pre(get_field('slideshow_header',$post_id) );
pre($blog['domain'] . ' - ' . $new_post_id);*/
/*
* duplicate all post meta just in two SQL queries
*/
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
pre($blog['blog_id'].' - '. $blog['domain'] . ' - ' . $new_post_id);
}
}
switch_to_blog($original_blog_id);
die;
} else {
//ERROR MSG NO HOTEL CHOSEN
pre("error_2"); die;
}
} else {
//ERROR MSG NO CATEGORY CHOSEN
//pre("error_1"); die;
}
return true;
}
add_action( 'save_post', 'push_posts_to_hotels', 10, 3 );