Skip to:
Content
Pages
Categories
Search
Top
Bottom

how do i delete all topics and replies for a user

  • @marisa2023

    Participant

    When the delete_user action is called I want to delete all the bbPress forum topics and replies for a specified user.

    I think the problem is the posts are having their user id set to 1 as part of the deletion process and by the time the delete_user action is called, it is too late.

    The code I have so far is given below. I’m not quite sure what the current state of this code is, but I think it deletes topics but not replies.

    // Get remaining replies by user (not already removed with topics)
    $replies = get_posts(array(
    ‘author’ => $user_id,
    ‘post_type’ => bbp_get_reply_post_type(),
    ‘post_status’ => array(‘publish’, ‘closed’, ‘spam’, ‘pending’, ‘trash’, ‘inherit’),
    ‘numberposts’ => -1,
    ‘fields’ => ‘ids’));

    // Delete replies
    if (!empty($replies))
    {
    foreach($replies as $reply_id)
    {
    bbp_delete_reply($reply_id, true);
    }
    }

    // Get all topics by user
    $topics = get_posts(array(
    ‘author’ => $user_id,
    ‘post_type’ => bbp_get_topic_post_type(),
    ‘post_status’ => ‘any’,
    ‘numberposts’ => -1,
    ‘fields’ => ‘ids’));

    // Delete topics (this also deletes their replies)
    if (!empty($topics))
    {
    foreach ($topics as $topic_id)
    {
    bbp_delete_topic($topic_id, true); // true = force delete
    }
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • @robin-w

    Moderator

    Presuming that you want to delete the user and any comments made in wordpress as well, then

    dashboard>users>select the user and press delete

    You will then be given the option to delete all content and the user.

    @marisa2023

    Participant

    I wanted to do it programmatically.

    @robin-w

    Moderator

    ok, so can you post the line of code code you are using to call that action please, or confirm that you are not using any reassign field in that call

    @marisa2023

    Participant

    I’ve got it to work now.

    I used the WordPress function wp_delete_post.

    @robin-w

    Moderator

    great – glad you are fixed!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar