• strictly-software

    (@strictly-software)


    What is the best SQL to use to DELETE post_tags AND only post_tags without deleting categories that may have the same name as the post_tag and therefore joined by a taxonomy record.

    I found this SQL on this site sometime ago and used it but it seems to cause problems if a post_tag exists that also has a category of the same name. OR I have people complaining it does.

    So if I only wanted to delete POST_TAGS where there were less than 4 articles using that tag I am currently using this SQL

    DELETE a,c
    								FROM	 wp_terms} AS a
    								JOIN	wp_term_taxonomy} AS c ON a.term_id = c.term_id
    								WHERE (
    										c.taxonomy = 'post_tag'
    										AND  c.count <= 3
    									);

    However if I switch the DELETE to a SELECT it only returns the post_tag records so looks ok to me.

    Should I only delete from the wp_terms table e.g remove the ,c from the DELETE statement or is it okay to delete both records?

    I thought the term_taxonomy record just joined the term up to the taxonomy so if I had a post_tag AND category using the same name e.g “Sales” there would be two term records and two term_taxonomy records.

    As I said when I switch it to a SELECT it seems to return the correct data e.g

    SELECT a.* , c.*
    from wp_terms as a
    JOIN	wp_term_taxonomy AS c ON a.term_id = c.term_id
    WHERE a.name='Sales'
    AND c.taxonomy = 'post_tag'

    Just returns a record from both tables for the post tag NOT the category record.

    I could add an extra LEFT JOIN in to return only records where the term doesn’t exist as another taxonomy type but can anyone see anything wrong with this or should I be leaving the term_taxonomy record alone and just deleting the from the terms table?

    It’s just that I had someone complain about missing “records”, not very clear what they are saying to be honest but it seems after running this SQL they have some kind of mess.

    Thanks for your help in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • kjodle

    (@kjodle)

    It’s just that I had someone complain about missing “records”, not very clear what they are saying to be honest

    Before you go messing about with the database, I would highly recommend that you figure out what they are saying. And then backup the database before you make any changes.

    Thread Starter strictly-software

    (@strictly-software)

    Thanks for replying, I do know what I am saying, sorry if it wasn’t clear but the question is in the first paragraph of my post e.g

    What is the best SQL to use to DELETE post_tags AND only post_tags without deleting categories that may have the same name as the post_tag and therefore joined by a taxonomy record?

    A) Its for a plugin and
    B) I always backup my database before a DELETE and
    C) I’m not the one deleting anything, however,

    I do want to know which SQL is best for deleting JUST post_tags as I asked.

    The other part of the post were just examples of SQL that I am using and trying to show what I mean through examples e.g turning the DELETE into a SELECT so nothing is DELETED. As doing that shows me nothing should happen to records marked as “category” if I did use the SQL I am currently.

    I cannot tell you what the person who is complaining actually did because it is pretty garbled. I have asked for clarification but have had none as of yet.

    However the question still remains e.g which SQL to use to delete JUST post tag records e.g if I wanted to delete all post tags that only belonged to less than 4 articles.

    So if the tag “Sales” was only tagged in 4 articles I want to delete it BUT there was also a category on the site called “Sales” as well.

    Therefore I want to delete the post tag SALES records BUT NOT any category SALES records.

    Thanks for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Best way to delete post_tags and ONLY post_tags’ is closed to new replies.