通过多站点中的自定义字段值对应的垃圾桶
问题描述:
我有一个多站点设置是自定义帖子类型article
与自定义字段在网络上的其他帖子有关alphanumeric_id
此关键在每个博客上都是唯一的,并且会生成相关帖子编程上add_action('wp_insert_post', 'bZive_create_TranslatedContent', 15, 3);
通过多站点中的自定义字段值对应的垃圾桶
function bZive_trash_TranslatedContent($post_id){
// Get the current blog id
$original_site_id = get_current_blog_id();
$postTypes = array('profile', 'article');
$postType = get_post_type(get_the_ID());
$randomString = get_post_meta(get_the_ID(), 'alphanumeric_id', true);
$args = array(
'public' => true,
'limit' => 500
);
$sites = wp_get_sites($args);
$tests = array();
foreach ($sites as $site) {
switch_to_blog($site['blog_id']);
if(get_current_blog_id() != $original_site_id and get_current_blog_id() != 1){
$args = array(
'post_type' => 'article',
'post_status' => array(
'publish', 'draft', 'pending','auto-draft', 'future', 'private'
),
'meta_query' => array(
array(
'key' => 'alphanumeric_id',
'value' => $randomString ,
'compare' => '=',
),
),
);
$posts = new WP_Query($args);
if($posts->have_posts()) {
while($posts->have_posts()) {
$posts->the_post();
// Move the post to trash
wp_trash_post(get_the_ID());
}
}
wp_reset_postdata();
}
restore_current_blog();
}
}
add_action('trash_post','bZive_trash_TranslatedContent',10,3);
我要归档什么:如果我提出一个岗位到垃圾桶 - 所有相关的帖子应该被移动到回收站了。但不知何故帖子并没有被删除 - 我测试了WP_Query它获得了正确的帖子,但仍然没有被删除。
答
终于在工作!
function bZive_trash_TranslatedContent($post_id){
// Get the current blog id
$original_site_id = get_current_blog_id();
$postTypes = array('profile', 'article');
$postType = get_post_type(get_the_ID());
$randomString = get_post_meta(get_the_ID(), 'alphanumeric_id', true);
if (in_array($postType, $postTypes) and empty(get_post_meta($post_id, 'del_translatedContent', true))) {
add_post_meta($post_id, 'del_translatedContent', 'true');
$args = array(
'public' => true,
'limit' => 500
);
$sites = wp_get_sites($args);
$tests = array();
foreach ($sites as $site) {
switch_to_blog($site['blog_id']);
if(get_current_blog_id() != $original_site_id and get_current_blog_id() != 1){
$args = array(
'post_type' => 'article',
'post_status' => array(
'publish', 'draft', 'pending','auto-draft', 'future', 'private'
),
'meta_query' => array(
array(
'key' => 'alphanumeric_id',
'value' => $randomString ,
'compare' => '=',
),
),
);
$posts = new WP_Query($args);
if($posts->have_posts()) {
while($posts->have_posts()) {
$posts->the_post();
// Move the post to trash
wp_trash_post(get_the_ID());
add_post_meta(get_the_ID(), 'del_translatedContent', 'true');
}
}
wp_reset_postdata();
}
restore_current_blog();
}
}
}
add_action('wp_trash_post','bZive_trash_TranslatedContent',1,3);