WordPress Database

Custom Database Tables

Database Tables

  • wp_commentmeta
    • meta_id
    • comment_id
    • meta_key
    • meta_value
  • wp_comments
  • wp_links
  • wp_options
  • wp_postmeta
    • Used for both posts AND pages. This is where custom fields attached to a post/page would store their data.
    • $wpdb->postmeta
    • meta_id
    • post_id
    • meta_key
    • meta_value
  • wp_posts
    • All content types are kept in the wp_posts table and are distinguished from one another by a post type. Default post types are post, page, attachment, revision, and navigation menu.
    • Navigation Menu – nav_menu_item – This is not an entire menu but a single navigational item on a larger menu.
  • wp_terms
    • For the taxonomy system, contains categories, tags, and custom taxonomies.
  • wp_term_relationships
    • When terms are hierarchical the relationship between one term and another is stored here. This table is being merged into wp_terms in the future.
  • wp_term_taxonomy
    • Used to determine which terms are actually the root level of a taxonomy.
  • wp_usermeta
  • wp_users

Methods for Saving Data to Database

  • Option Mechanism – For small amounts of data that rarely changes.
    • Best practice is to use as few as possible, consider including multiple values in a single option field.
    • add_option($name, $value, $deprecated, $autoload);
      • Only creates if it doesn’t already exist
      • $(option_)name = name of option; $value = default value (optional); $deprecated = no longer used, pass an empty string or null to it if you want to use $autoload; $autoload = should option be loaded by wp_load_alloptions function? by default is yes.
    • getoption($option);
      • Retrieve the option from DB.
    • update_option($option_name, $newvalue);
      • Can be used to create an option if the deprecated or autoload params are not needed.