duandongjin5647 2017-10-04 09:28
浏览 30
已采纳

WordPress:删除一个帖子后删除用户的所有帖子

I'm using serveral Custom Post Types in WordPress. Every user can add a company profile as a specific post type called "company". If the user has a company profile he can add more content like jobs, events and more. All defined as specific Custom Post Types.

It is also possible that the user deletes his own company profile. If he do so, all other posts from the user should be deleted as well (except normal posts) and the URL should be redirected (301) to the homepage.

I've found the action "delete_post" in the WP docs (https://codex.wordpress.org/Plugin_API/Action_Reference/delete_post). But I'm not sure how to fire it.

Does anyone has an hint?

EDIT: See my answer below

  • 写回答

3条回答 默认 最新

  • dongqixuan3112 2017-10-04 15:27
    关注

    I found the solution. The problem was the action 'delete_post'. I've changed it to 'trash_to_publish' based on the Post Status Transitions: https://codex.wordpress.org/Post_Status_Transitions

    Now everything works fine.

    /**
     * Deletes all posts from author of company profile.
     */
    function delete_all_posts_from_author($post_id) {
    
    
        global $post;
        $id = $post->ID;
    
        // Only trigger if post type is "company"
        if ( get_post_type($id) == "company" ) {
    
            $author_id = $post->post_author;
    
            $posts_from_author = get_posts(
                array(
                    'posts_per_page'    => -1,
                    'post_status'       => 'publish',
                    'post_type'         => array('event','job'),
                    'author'            => $author_id,
                    'fields'            => 'ids', // Only get post ID's
                )
            );
    
            foreach ( $posts_from_author as $post_from_author ) {
                wp_trash_post( $post_from_author, false); // Set to False if you want to send them to Trash.
            }
        }
    
    
    }
    
    add_action( 'publish_to_trash',  'delete_all_posts_from_author', 10, 1 );
    

    As a bonus I could use the function to publish all posts from a user again if I untrash the company profile.

    /**
     * Untrash posts if company profile is untrashed
     */
    function untrash_all_posts_from_author($post_id) {
    
    
        global $post;
        $id = $post->ID;
    
        if ( get_post_type($id) == "company" ) {
    
            $author_id = $post->post_author;
    
            $posts_from_author = get_posts(
                array(
                    'posts_per_page'    => -1,
                    'post_status'       => 'trash',
                    'post_type'         => array('event','job'),
                    'author'            => $author_id,
                    'fields'            => 'ids', // Only get post ID's
                )
            );
    
            foreach ( $posts_from_author as $post_from_author ) {
                wp_untrash_post( $post_from_author); // Set to False if you want to send them to Trash.
            }
        }
    
    
    }
    
    add_action( 'trash_to_publish',  'untrash_all_posts_from_author', 10, 1 );
    

    Hope that helps. And please let me know if I've made a mistake.

    EDIT: I've changed the argument wp_delete_post() to wp_trash_post() because wp_delete_post() only applies to native posts, pages, and attachments. Great answer from @rarst here: https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?