wp_term_relationships

Published: 3 Mar 2023

The wp_term_relationships table is used to associate WordPress posts, pages, or any custom post type with the terms from the wp_terms and wp_term_taxonomy tables.

Each row stores an object_id (post or page ID), term_taxonomy_id (identifying the term), and term_order in which the associated terms are displayed for the object.

Columns

object_id: This column stores the unique identifier for the object that is being associated with a term. The object can be a post, page, or any custom post type in WordPress. This value corresponds to the ID column in the wp_posts table. For example, if you have a post with an ID of 123, and you associate it with a term with a term_taxonomy_id of 456, then you would see a row in the wp_term_relationships table with object_id of 123 and term_taxonomy_id of 456.

term_taxonomy_id: This column stores the unique identifier for the term. This value corresponds to the term_taxonomy_id column in the wp_term_taxonomy table. It is not the term_id from wp_terms table, which may be a bit confusing.

term_order: This column stores the order in which terms are displayed for a particular object (e.g. post). This value is an integer, with lower values indicating a higher priority (displayed first). For example, if you have a post that is associated with multiple categories, you can use the term_order value to control the order in which the categories are displayed.

Example

For example, let's say you have a blog with multiple categories, including "News", "Recipes", and "Tips & Tricks".

You create a new post, which gets post ID of 135 in wp_posts table. You assign this new post to the "News" and "Recipes" categories.

WordPress creates a new row in the wp_term_relationships table with object_id = 135 (the post ID), term_taxonomy_id = the "News" category's term_taxonomy_id, and term_order = 1 (since "News" is the first category assigned to the post).

Another row is created with object_id = 135 (still the same post ID), term_taxonomy_id = "Recipes" category term_taxonomy_id, and term_order = 2 (since "Recipes" is the second category assigned to the post).

When the post is displayed on the front-end, the categories will be displayed in the order specified by the term_order value: "News", "Recipes".

SQL Schema

CREATE TABLE `wp_term_relationships` (
`object_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`term_order` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`object_id`,`term_taxonomy_id`),
KEY `term_taxonomy_id` (`term_taxonomy_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

By remaining on this website or using its content, you confirm that you have read and agree with the Terms of Use Agreement.

We are not liable for any damages resulting from using this website. Any information may be inaccurate or incomplete. See full Limitation of Liability.

Content may include affiliate links, which means we may earn commission if you buy on the linked website. See full Affiliate and Referral Disclosure.

We use cookies and similar technology to improve user experience and analyze traffic. See full Cookie Policy.

See also Privacy Policy on how we collect and handle user data.

© 2024 WPDir