Forums

How to delete default category via database? (12 posts)

  1. Businessman
    Member
    Posted 6 years ago #

    I'm using WP as a content management solution, but I need to get rid of a couple of categories and their entries.

    Is it possible to delete entire categories and their contents via a database query?

    For example, can I send all posts for deletion to the "default" category, then delete that category - and all posts under it - via a database query?

    Any replies very much appreciated, and apologies if the question seems basic.

  2. MichaelH
    Volunteer
    Posted 6 years ago #

    If there aren't many posts, you could manually delete the records in wp_posts and wp_post2cat. But assuming that's not feasible then here's two sql statements that should do the job. This assumes you want to delete posts with Category ID=2

    1. Backup your database
    2. Make sure you backup your database
    3. I used phpMyAdmin to execute these two sql statements:

    DELETE * FROM
    wp_post2cat WHERE category_id=2;
    DELETE FROM wp_posts USING wp_posts p1 LEFT JOIN wp_post2cat p2 ON p1.ID = p2.post_id
    WHERE p2.post_id IS NULL;

    4. Verify it worked and if any question it worked restore your database!

    (EDIT) Then you can delete the actual Category you don't want.

  3. Businessman
    Member
    Posted 6 years ago #

    Hi Michael -

    Many thanks for the reply - I'll get that tested out this week. :)

    - Brian

  4. BillyG
    Member
    Posted 6 years ago #

    Can I delete the "uncategorized" from wp_posts and wp_post2cat?

    I really came looking for a way to not display cats at all in each post, but of course I still want to build on them, is that possible?

    If not the second, the first would be nice. TIA.

  5. moshu
    Member
    Posted 6 years ago #

    Don't delete anything from those tables - it will screw up your blog!

    If you don't like the uncategorized, you can:
    - re-name it to whatever you want
    - select another category to be the default

    As for displaying a post's category or not - that's a theme issue: edit your theme files so they don't have that specific template tag (the_category).

    And don't forget! DO NOT touch those tables in the DB...

  6. BillyG
    Member
    Posted 6 years ago #

    gotcha bud, thx

    EDIT:

    If you don't like the uncategorized, you can:
    - re-name it to whatever you want
    - select another category to be the default

    I renamed the slug for "uncategorized" (couldn't rename the actual name) and even put another category as parent over it (which does work for the posttime cat drop-down list) but it is still named "uncategorized" and checked as the default...

  7. BillyG
    Member
    Posted 6 years ago #

    bump

  8. BillyG
    Member
    Posted 6 years ago #

    as usual, a quick answer, but not the right one, besides, I'm using the default theme until others get 2.0 compliant...

  9. Businessman
    Member
    Posted 6 years ago #

    Just to update - I tried running the following queries in phpmyadmin:

    DELETE * FROM wp_post2cat WHERE category_id=2;
    DELETE FROM wp_posts USING wp_posts p1 LEFT JOIN wp_post2cat p2 ON p1.ID = p2.post_id
    WHERE p2.post_id IS NULL;

    However, I receive the message:

    You have to choose at least one column to display

    Not quite sure how to proceed from here. I would be most grateful for any help.

  10. pietrosperoni
    Member
    Posted 6 years ago #

    Hmm, Moshu, exactly what should go bad if I play with the categories in the DB? bacause it is already a few days that I am, and it seem to work all fine.

    I have:
    (1)deleted all categories that were refered to by no post
    (2)delete all multiple references from the same post to the same category (don't ask me why the same category would appear more than one time)
    (3)deleted all reference from any post to a certain category and then did (1) again.

    I tried to act reasonable, but I had some 200 categories that I had to clear, and there were those multiple references to the same category. In short, now it seems that it works, but you made me a bit hack-conscious of what I did. After all it was the first time that I played around with mysql.

    BTW, I am using phpMyadmin.

    It seem to be working fine. But there is anything in particular I need to check?

    Many thanks,
    Pietro

  11. Businessman
    Member
    Posted 6 years ago #

    pietrosperoni, if you don;t my my asking, what query did you run in phpmyadmin to:

    - delete a category by ID?
    - delete all posts belonging to a category ID?

    Most appreciated indeed if you can answer that.

  12. pietrosperoni
    Member
    Posted 6 years ago #

    ATTENTION: I never deleted any post. Only categories and links cat2post

    I don't mind, but I don't think I can help much. In Phpmyadmin there is a nice user interface that let you act using checkboxes, and since I had 90 posts in all, it was quite easy. Just long.

    Maybe give a look at the function wp_delete_post()

    ( http://cenamayo.com/wpxref/nav.html?_functions/index.html )

    to know what you need to do to delete a post (example, delete also the comments)

    How many posts do you have to delete?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags