Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Saving Post Data in Another Database

This is possible, but you’re going to have to do a lot of custom work to get what you want. If you want all of your postmeta to come from and go to a separate database, you can use these filters:

  • get_{$meta_type}_metadata – This filter is documented in wp-includes/meta.php. Returning a non-null value from this filter will short-circuit calls to get_metadata (used by get_post_meta).

  • update_{$meta_type}_metadata – This filter is also in wp-includes/meta.php. Returning a non-null value will short-circuit the update_metadata function, called by update_post_meta.

  • add_{$meta_type}_metadata – Effectively the same as update_{$meta_type}_metadata, except it’s called for add_post_meta.

  • delete_{$meta_type}_metadata – Same as all of the above except used to delete metadata.

Using these four filters, you can create a custom connector to another dataset instead of using the wp_postmeta table.

<?php
class MyCustomData {
    public static $instance = null;

    private $connection = null;

    // Create a singleton instance of our class.
    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function __construct() {
        $this->init_connection();
        $this->hooks();
    }

    private function init_connection() {
        // Do whatever you need to here to create your new database connection.

        // For example, you could open a PDO connection... http://php.net/manual/en/pdo.connections.php
        $this->connection = new PDO('connection string here');
    }

    // Define your meta overrides.
    private function hooks() {
        add_filter( 'add_post_metadata', [ $this, 'add_meta' ], 10, 5 );
        update_filter( 'update_post_metadata', [ $this, 'update_meta' ], 10, 5 );
        delete_filter( 'delete_post_metadata', [ $this, 'delete_meta' ], 10, 5 );
        get_filter( 'get_post_metadata', [ $this, 'get_meta' ], 10, 5 );
    }

    /**
     * From the filter documentation in meta.php
     *
     * @param null|bool $check      Whether to allow adding metadata for the given type.
     * @param int       $object_id  Object ID.
     * @param string    $meta_key   Meta key.
     * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
     * @param bool      $unique     Whether the specified meta key should be unique
     */
    public function add_meta( $check, $object_id, $meta_key, $meta_value, $unique ) {

        // There may be cases where you _don't_ want your data going to the remote database.
        // If that happens, return null and WP will continue on it's merry way.
        if ( ! $this->allow_remote_db_action( $object_id ) ) {
            return null;
        }

        // Whatever logic you need to do here and then...
        $result = $this->connection->query('INSERT INTO ...' );

        // Returning non-null will stop WP from doing it's own thing.
        if ( $result ) {
            return $result;
        }

        // Return null to let WP go on about it's business adding post meta.
        return null;
    }

    /**
     * The other three methods are effectively the same structure: 
     * - Prepare your data
     * - Make an action against the remote database.
     * - Return null if things didn't go well and you want WP to take over, or non-null
     * if you got the result you wanted and don't need WP to do what it was doing.
     */
}

// Kick off our class and set up the filters for post meta.
add_action( 'init', 'MyCustomData::get_instance' );

I strongly advise that you properly “gate” the functions you setup, meaning that you should only be getting and sending what you need to and from the remote database, and allow WordPress to handle the rest of the postmeta. There’s probably some Bad Stuff that can come out of a system like this, so be prepared for headaches and debugging sessions.

Related Posts:

  1. Sanitizing `wp_editor();` Values for Database, Edit, and Display
  2. Update all posts automatically when using post_meta
  3. Better post meta efficiency?
  4. get_post_custom stripping styling issue
  5. Change post format using custom field
  6. Exporting Data from WordPress into a flat table
  7. How to get Advanced Custom Field Value According using POST ID? [closed]
  8. What Is meta_id In wp_postmeta?
  9. Add custom field automatically (add_post_meta) with value based on number of words of article
  10. Guest Author – How to display posts on /author/ archive page
  11. How can I sort posts by the date and a custom meta field?
  12. How do I correctly set up a WP-Query to only show upcoming event-posts?
  13. Cannot retrieve a custom RSS field from posts
  14. Saving custom fields to a custom taxonomy
  15. Customize rel=canonical tag for single blog post
  16. How to I retrieve the ID from the Posts page?
  17. Add field to user meta table in database when link is clicked
  18. if in category but only with post meta
  19. Update post meta within save_post action
  20. Display post number by category
  21. Add multiple meta keys to a post at once
  22. WP_Query custom field pass the post id
  23. If custom field is empty, use one from a previous post
  24. saving/reading custom field value does not work – no value gets POSTed
  25. What snippet do I need to type to show my ACF field show up on my theme?
  26. Post meta not working
  27. Is there a way to save different data when USER interacts with the same POST?
  28. How do you update post date (year only) in a separate custom field?
  29. WP_Query sort by meta_value_num or date
  30. Creating Ordered Query using Meta_key
  31. Auto populate custom fields by post date
  32. ACF Relationship – Get Parent’s Post Object
  33. Run function after post is updated
  34. How enter custom post meta or custom field on box post list archive page?
  35. How to make a local “scroll to ID” on post?
  36. How to detect if an ACF custom-field is really in use?
  37. What effect can a large wp_post table have on overall site performance?
  38. How to make internal links creating plugin to respect ACF?
  39. Add postmeta to all blog posts
  40. How can I stop ‘in use’ message from intermittently blocking my wp_posts table?
  41. How to get value of a selected option from select tag and use it in WP_query to filter posts?
  42. When working with a post, almost all wp_postmeta are deleted
  43. Assign a day of the week to post, e.g: Assign Monday to post and have it only appear when the day is Monday
  44. Bulk Post update_post_meta
  45. More Than 50K Categories and WordPress Admin Panel Stop Showing Categories and Posts
  46. Re-order Category Meta-data
  47. how i show manual data in a post
  48. How to hide meta from search result only on pages?
  49. How do I display posts ordered by a date custom field?
  50. Clean up customize_changeset in DB
  51. Grab meta data before post is saved
  52. How do I get a certain set of posts from the database?
  53. How to fetch posts that are stored in the different table than the default table (wp_posts)?
  54. Maximum number of posts per page before affecting performance?
  55. Automatically convert standard posts with custom fields to custom post types
  56. Check if user has avatar
  57. SQL trigger failes with post_content
  58. alt of attached image in post / pages
  59. WordPress Request Post All Post ID in a Loop? [closed]
  60. Best Way to detect unique posts in wp rest api
  61. Get the correct meta_value with get_post_meta
  62. Surrogate ID for posts, is there an alternative field in the posts table?
  63. Extracting a TLD from the content and assign to custom field
  64. post_name is not stored until post is published… where should I store my slug until it goes live then?
  65. Filter question list on substring of metavalue
  66. Add a meta to a post submited from a frontend form
  67. get backup file from wordpress database in x days
  68. Query Posts based on custom field value
  69. How can I set and update the the_date according to a custom field of the post
  70. How to retrieve a post by inputing the url in a custom field and displaying it on an options page
  71. Expired Post with More Recent Time Stamp?
  72. Custom fields in normal posts
  73. update_post_meta not working in function
  74. Converting Posts to Pages
  75. Show posts between two Custom Fields date
  76. Add custom class to existing menu items from custom meta
  77. WP Query between posts custom fields [duplicate]
  78. get_the_ID() fails the first time, returns a value the second time it’s called
  79. I moved my site to another server, wp admin works so does the front page, but posts don’t work
  80. How to get only current images of a post
  81. Ordering posts alphabetically by meta keys and title
  82. How to change the publishing date of each posts?
  83. Can’t seem to replicate permalink structure for localhost development site
  84. Extra field in wp_post instead of postmeta
  85. Voting System, database connections?
  86. Where should get_post_meta() go to get $post->ID get_post_meta() is empty and $post->ID & get_the_id() are working
  87. When I click edit on a post, all the content disappear. Does anyone know how to fix this?
  88. Using Post ID and Page ID in same function
  89. Apply comment to different post (not the current post) [duplicate]
  90. Adding custom fields to the Quick Edit screen – puzzled about the column concept
  91. Order posts by separate menu order for different sub-categories
  92. Given two custom post types: Automatically add meta fields from one custom post type to another
  93. How to only allow post to be deleted if custom field doesn’t exist
  94. How to custom sort-out editing-panel fields?
  95. Delete old post with new post
  96. How can I create a menu items from meta box based on users input
  97. get_post_meta property of non object
  98. Home page: using custom fields for posts and pages
  99. How would I attach media/images to a post based on a ID stored in a each post’s custom field
  100. How do I insert a custom field in a user submitted post?
Categories posts Tags advanced-custom-fields, custom-field, database, post-meta, posts
Can I Create a Second Admin Level User Role?
Custom query vars filters problem with pagination

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress