【问题标题】:WordPress: Delete all Posts from user after one post has been deletedWordPress:删除一篇帖子后删除用户的所有帖子
【发布时间】:2017-10-05 01:06:48
【问题描述】:

我在 WordPress 中使用多个自定义帖子类型。 每个用户都可以将公司简介添加为称为“公司”的特定帖子类型。 如果用户有公司资料,他可以添加更多内容,如工作、活动等。全部定义为特定的自定义帖子类型。

用户也有可能删除自己的公司资料。 如果他这样做了,该用户的所有其他帖子也应该被删除(普通帖子除外),并且应该将 URL 重定向 (301) 到主页。

我在 WP 文档 (https://codex.wordpress.org/Plugin_API/Action_Reference/delete_post) 中找到了“delete_post”操作。但我不知道如何开火。

有人有提示吗?

编辑:在下面查看我的答案

【问题讨论】:

    标签: php wordpress api custom-post-type


    【解决方案1】:

    我找到了解决方案。问题是“delete_post”操作。 我已根据帖子状态转换将其更改为“trash_to_publish”:https://codex.wordpress.org/Post_Status_Transitions

    现在一切正常。

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

    作为奖励,我可以使用该功能再次发布用户的所有帖子,如果我取消删除公司资料。

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

    希望对您有所帮助。如果我犯了错误,请告诉我。

    编辑:我已将参数wp_delete_post() 更改为wp_trash_post(),因为wp_delete_post() 仅适用于本地帖子、页面和附件。来自@rarst 的好答案:https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888

    【讨论】:

    • 看起来不错 :) 唯一的事情是 wp_untrash_post() 只接受一个参数,但这是可靠的。
    • 与@DanielH.J. 相同。说-但对于wp_trash_post(),@Cray。它只需要 1 个参数,即 post_id。我想你可能对wp_delete_post() 感到困惑?
    【解决方案2】:

    这是可能的。 公司是帖子类型,因此剩余内容将是分类法,因此当您在元数据中删除帖子时,分类法不会被删除。

    <?php
    add_action( 'admin_init', 'codex_init' );
    function codex_init() {
        add_action( 'delete_post', 'codex_sync', 10 );
    }
    
    function codex_sync( $pid ) {
        global $wpdb;
        if ( $wpdb->get_var( $wpdb->prepare( 'SELECT post_id FROM .'$wpdb->prefix.'postmeta WHERE post_id = %d', $pid ) ) ) {
            $wpdb->query( $wpdb->prepare( 'DELETE FROM '.$wpdb->prefix.'postmeta WHERE post_id = %d', $pid ) );
        }
    }
    ?>
    

    【讨论】:

    • 谢谢,但剩下的内容也是帖子类型,不是分类法
    【解决方案3】:

    类似:

    // Add action trigger
    add_action( 'delete_post', 'delete_other_custom_posts', 10 );
    
    function delete_other_custom_posts( $post_id ) {
        global $wpdb;
        $post = get_post( $post_id );
    
        // Only trigger if post type is "company"
        if ( $post->post_type == "company" ) {
            $author_id = $post->post_author;
    
            // Find other custom posts' ids by the same user
            $posts_to_delete = $wpdb->get_col(
                $wpdb->prepare(
                    "SELECT ID FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_author = %d AND post_type IN (<put your custom post types' slugs here>)",
                    $author_id
                ), ARRAY_A
            );
    
            if ( $posts_to_delete ) {
                foreach ( $posts_to_delete as $post_delete_id ) {
                    // Delete the custom post
                    wp_delete_post( $post_delete_id );
                }
            }
    
            // Redirect
            wp_redirect(<page you want to redirect to>);
            exit;
        }
    }
    

    【讨论】:

    • 我会试试的。谢谢!
    • 嗨,我添加了这样的 post-type slug:"post_type IN ('event','job')" 但不幸的是它不起作用?!
    • 也许是因为函数 wp_delete post 不适用于自定义帖子类型? WP 文档说“删除或删除帖子、附件或页面。”
    • 自定义帖子类型也是帖子,只是名称不同。您在 PHP 的错误日志中看到任何错误吗?试试 var_dump( $posts_to_delete ) 看看返回了什么。
    • 嗨丹尼尔,你可以在下面看到我的回答。我认为是采取行动“delete_post”
    猜你喜欢
    • 2021-07-09
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    相关资源
    最近更新 更多