SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Wordpress Categories

    What table in wordpress determines which category each post is in?

    Thank you,
    David
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sitedesigner View Post
    What table in wordpress determines which category each post is in?
    The category information is held in three related tables.

    The "wp_terms" table holds the actual category definitions, with an id, a name, and a description (along with tag ids and names).

    The "wp_terms_taxonomy" table identifies the "term" type - category or tag.

    The "wp_terms_relationships" table matches up a post with a category or tag.

    It's a bit confusing at first, until you realize that they made tags and categories identical from the DB end. They just have different functionality in the front end.

    Good luck,
    - Walkere

  3. #3
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok

    I have a script that makes posts in wordpress but has nothing to define category yet.

    How would I then insert something into the wp_terms_relationships?

    Thanks!

    -David
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner

  4. #4
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got something like this right now

    PHP Code:
    $post_ID wp_insert_post $post_data );
    $sql 'INSERT INTO `rl_blog`.`wp_term_relationships` (`object_id`, `term_taxonomy_id`) VALUES (\''.$post_ID.'\', \'31\');'
    but I'm not quite sure how to do it because that doesnt work.
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sitedesigner View Post
    but I'm not quite sure how to do it because that doesnt work.
    I just tested it out in phpmyadmin, and this query worked for me.

    Code:
     INSERT INTO wp_term_relationships( object_id, term_taxonomy_id )
    VALUES (
    -1, -1
    )
    So in php, you could build the query like so...
    PHP Code:
    $object_id = -1;  //  Replace with correct id
    $term_id = -1;  // Replace with correct id

    $query "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES ($object_id$term_id)"
    I'm not sure exactly what is wrong with your query, though. What is rl_blog for? Is that a table prefix in your db?

    I also find it useful to build queries using double-quotes in php, so that a) I don't have to escape single-quotes when I use them, and b) I can directly include a variable without using string concatenations. Makes more readable queries, IMO.

    - Walkere

  6. #6
    SitePoint Evangelist sitedesigner's Avatar
    Join Date
    May 2005
    Location
    On the Computer
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok.

    rl_blog is the name of a database.
    █ [o] Programmer? PM me!
    █ [o] Getting back into the Business
    █ [o] Sitedesigner


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •