我正在尝试为 WPMU 安装创建一个集中的帖子传递系统。
设置:
- 最新的 WordPress 安装
- 插件:高级自定义字段 (ACF)
- 插件:WPML
目标:
我想创建一个帖子,最终应该将其交付给我之前选择的 WPMU 安装。
问题1:
我无法将帖子缩略图正确保存到新创建的帖子中。
问题2:
正如我已经提到的,我的 WPMU 中有几个安装。因此,还有大量具有不同字段键的 ACF 字段。有谁知道是否有一种简单的方法可以将帖子中的字段值获取到新创建的帖子中?
他们在 ACF-Forums 中写道,我应该使用字段键来处理这个问题。但我不想手动通过每个站点、语言来获取字段键。
到目前为止的代码:
/* 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 );