• I have a thousand posts in Category X. I want to assign a tag Y to all posts of the category X. Is this possible with any SQL query or plugin?

    I don’t want to first filter the posts from Edit > Posts and then manually assign tag to each post. It will take a long time and very painful.

    Please help

Viewing 7 replies - 1 through 7 (of 7 total)
  • BE SURE TO BACK UP YOUR DATABASE FIRST!!

    1. Create the new tag.

    2. Get the new tag’s term_taxonomy_id (assume it is 4 for this example)

    3. Get the category’s term_taxonomy_id (assume it is 55 for this example)

    4. Create a table from all rows in wp_term_relationships where
    the term_taxonomy_id is 55.

    CREATE TABLE temp
    SELECT * FROM wp_term_relationships
    WHERE term_taxonomy_id = 55

    5. Insert a term_relationships row for each row in the temp table.

    INSERT IGNORE INTO wp_term_relationships
    SELECT temp.object_id, 4, 0
    FROM temp
    LEFT JOIN wp_posts p ON (p.ID = temp.object_id)
    WHERE p.post_type = ‘post’
    AND p.post_status = ‘publish’

    Thread Starter ampercent

    (@ampercent)

    Didn’t worked.

    1st query executes successfully, when I run the second one, it shows “0 rows inserted”

    Your help is much appreciated.

    Thread Starter ampercent

    (@ampercent)

    Another thing which I noticed is that some of my tags and categories have the same ID

    Example:

    I have a category named “Internet” as well as a tag named “Internet”. Both are having the ID 3.

    Before running your code, I deleted the “Internet” tag and created a new tag with the same name but a different URL ( e.g Internet-tips). This time the tag ID was 557. If I ever create a new tag and name the slug as “Internet”, the ID becomes 3 which is equal to the ID of the “Internet” category.

    I hope my explanation is not confusing.

    Be sure you are using the term_taxonomy_id, not term_id for both the tag and the category.

    The term_id is assigned to a slug, which is determined by the name. So, a Tag with the name ‘Internet’ will have the same term_id as a Category with the name ‘Internet’. But their term_taxonomy_id will be different.

    Thread Starter ampercent

    (@ampercent)

    When I hover over a Category name’s “Edit” link, I get this URL

    https://www.domain.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&post_type=post&tag_ID=4

    When I hover over a Tag name’s “Edit” link, I get this URL

    https://www.domain.com/wp-admin/edit-tags.php?action=edit&taxonomy=post_tag&post_type=post&tag_ID=4

    Now, how do i get the correct ID’s as you said .

    You need to use phpMyAdmin (or another similar tool) to look in the wp_term_taxonomy table.

    For the Tag, find the row with the term_id = 4 and taxonomy = ‘post_tag’ and use that row’s term_taxonomy_id.

    For the Category, find the row with the term_id = 4 and taxonomy = ‘category’.

    Thread Starter ampercent

    (@ampercent)

    Hm….

    I used phpmyadmin for backing up the DB and then running the query. Thanks, will give it a try, your help is much appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Assign A Tag To All Posts Of a Category’ is closed to new replies.