【问题标题】:Trash realated post by custom field value in multisite通过多站点中的自定义字段值垃圾邮件
【发布时间】:2017-10-14 20:18:54
【问题描述】:

我有一个多站点设置,自定义帖子类型 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,它得到了正确的帖子,但仍然没有被删除。

【问题讨论】:

    标签: multisite recycle-bin


    【解决方案1】:

    终于成功了!

    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);
    

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2018-06-21
      • 1970-01-01
      相关资源
      最近更新 更多