如何删除基于‘user_meta’的所有用户和帖子?

时间:2016-12-16 作者:Prasad Patel

我开发了一个小型wordpress应用程序,其中包含机构(wp用户)、培训师(wp用户)、受训人员(wp用户)、课程(自定义帖子)和通知(自定义帖子)。所有的申请都很好,但如果我删除了一个机构,所有信息都属于该机构,如培训师、实习生、通知和;还应删除课程。当我创建“institute”用户时,我在wordpress表“wp\\U users”中存储所有相关信息,如名字、姓氏,“institute name”在“wp\\U usermeta”表中存储为“user meta”,键为“InInstitute\\U name”,如下是我的代码:

  $user_data = array(
            \'ID\' => \'\',
            \'user_pass\' => \'\',
            \'user_login\' => $first_name,
            \'user_email\' => $user_email,
            \'first_name\' => $first_name,
            \'last_name\' => $last_name,
            \'role\' => \'admin\'//get_option(\'default_option\')
        );
        $random_password = wp_generate_password(8,false);
        $user_id = wp_insert_user( $user_data );
        update_user_meta( $user_id, \'inistitute_name\',$insititute_name );
创建培训师(或)实习生时,我的代码如下:

  $user_data = array(
               \'ID\' => \'\',
               \'user_pass\' => \'\',
               \'user_login\' => $first_name,
               \'user_email\' => $trainer_email,
               \'first_name\' => $first_name,
               \'last_name\' => $last_name,
               \'role\' => \'trainer\'
         ); 
        $random_password = wp_generate_password(8,false);
        $user_id = wp_insert_user( $user_data );
        update_user_meta( $user_id, \'inistitute_name\',$this->institute_name[0] );
        wp_set_password($random_password, $user_id);
创建课程时,我的代码如下所示:

  $user_ID = get_current_user_id();
  $institute_name = get_user_meta($user_ID, \'inistitute_name\', true);
  $post = array(
            \'post_title\'    => $title,
            \'post_content\'  => $description,
            \'post_status\'   => \'publish\', 
            \'post_type\' => "courses"  
        );
  $id = wp_insert_post($post);
  update_post_meta( $id, \'inistitute_name\', $institute_name );
假设我们说,如果一个机构“abcd”被删除,那么所有信息,如“培训师”、“受训者”、“课程”和还应根据“user\\u meta”字段删除与该机构关联的通知。是否可以删除?(或者)我有没有做错什么?谁能告诉我我做错了什么?

更新时间:

  add_action( \'delete_user\', \'cyb_delete_institute_info\' );
function cyb_delete_institute_info( $id ) {

$user = get_userdata( $id );

if( ! empty( $user ) && in_array( "admin", (array) $user->roles ) ) {

    // Deleted user is admin, check `institute_name` meta field
    $institute_name = get_user_meta( $id, \'inistitute_name\', true );

    if( ! empty( $institute_name ) ) {
        // Deleted user is an admin and has `institute_name` meta,
        // so, it is a "Institute" user

        // Get posts to delete
        $posts_args = array(
            \'post_type\' => \'notifications\',
            \'meta_query\' => array(
                \'key\'   => \'inistitute_name\',
                \'value\' => $institute_name
            ),
        );

global $wpdb;
$querystr = "DELETE FROM $wpdb->wp_dxwe_posts LEFT JOIN $wpdb->wp_dxwe_postmeta ON $wpdb->wp_dxwe_postmeta.post_id = $wpdb->posts.ID WHERE $wpdb->wp_dxwe_postmeta.meta_key = \'inistitute_name\' AND 
        $wpdb->wp_dxwe_postmeta.meta_value = \'$institute_name\' AND $wpdb->wp_dxwe_posts.post_type = \'courses\' ";
$wpdb->query( $querystr );

        $posts_args2 = array(
            \'post_type\' => \'courses\',
            \'meta_query\' => array(
                \'key\'   => \'inistitute_name\',
                \'value\' => $institute_name
            ),
        );

        $query = new WP_Query( $posts_args );
        $query2 = new WP_Query( $posts_args2 );
        $posts_to_delete = $query->get_posts();
        $posts_to_delete2 = $query2->get_posts();

        foreach( $posts_to_delete as $post ) {
            wp_delete_post( $post->ID );
        }

        foreach( $posts_to_delete2 as $post ) {
            wp_delete_post( $post->ID );
        }

        // Get users to delete
        $users_args = array(
            \'role__in\'       => array( \'trainer\', \'trainee\' ),
            \'meta_query\' => array(
                \'key\'   => \'inistitute_name\',
                \'value\' => $institute_name
            ),
        );
        $query = new WP_User_Query( $users_args );
        $users_to_delete = $query->get_results();

        foreach( $users_to_delete as $user ) {
            wp_delete_user( $user->ID );
        }

    }

}
}
我写了两个单独的WP\\u查询来分别删除自定义post\\u类型,但都不起作用。相同的结果仅通知正确删除,但对于课程,其他学院课程也会被删除。

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

因为“institute”是一个用户,您希望在删除它时执行一些操作,所以您可以使用delete_user 操作(因为我们需要删除用户的信息,deleted_user 可能不起作用,对此不确定,但delete_user 应该可以肯定)。在这个钩子中,您可以根据inistitute_name 元字段并删除它们。

查看您的代码,可以通过用户角色(您正在分配)识别“Institute”用户类型admin) 和用户元inistitute_name. 所以,在delete_user 操作您可以检查删除的用户是否为和admin,以及是否institute_name 分配,在这种情况下,删除的用户是一个“机构”,您可以获得所有其他关联的用户和帖子。

add_action( \'delete_user\', \'cyb_delete_institute_info\' );
function cyb_delete_institute_info( $id ) {

    $user = get_userdata( $id );

    if( ! empty( $user ) && in_array( "admin", (array) $user->roles ) ) {

        // Deleted user is admin, check `inistitute_name` meta field
        $institute_name = get_user_meta( $id, \'inistitute_name\', true );

        if( ! empty( $institute_name ) ) {
            // Deleted user is an admin and has `institute_name` meta,
            // so, it is a "Institute" user

            // Get posts to delete
            $posts_args = array(
                \'post_type\' => array( \'courses\', \'notifications\' )
                \'meta_query\' => array(
                    \'key\'   => \'inistitute_name\',
                    \'value\' => $institute_name
                ),
            );

            $query = new WP_Query( $posts_args );
            $posts_to_delete = $query->get_posts();

            foreach( $posts_to_delete as $post ) {
                wp_delete_post( $post->ID );
            }


            // Get users to delete
            $users_args = array(
                \'role__in\'       => array( \'trainer\', \'trainee\' ),
                \'meta_query\' => array(
                    \'key\'   => \'inistitute_name\',
                    \'value\' => $institute_name
                ),
            );
            $query = new WP_User_Query( $users_args );
            $users_to_delete = $query->get_results();

            foreach( $users_to_delete as $user ) {
                wp_delete_user( $user->ID );
            }

        }

    }
}
注意:没有测试,只是作为示例代码写在这里。

相关推荐

set a user-meta key as avatar

我有一个名为“meta kay”的用户;阿凡达;;此值包含用户上载的图像的URL。如何将此元值设置为用户化身?我使用此代码,但不工作add_filter( \'get_avatar\', \'slug_get_avatar\', 10, 5 ); function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) { //If is email, try and find user ID