垃圾箱通过多站点中的自定义字段值实现帖子



我有一个多站点设置,是自定义帖子类型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);

相关内容

  • 没有找到相关文章

最新更新