这是有可能的,但你必须做大量的定制工作才能得到你想要的。如果希望Posteta的所有内容都来自并转到单独的数据库,可以使用以下过滤器:
get_{$meta_type}_metadata
- 此过滤器记录在wp-includes/meta.php
. 从此筛选器返回非null值将使对的调用短路get_metadata
(使用人get_post_meta
).update_{$meta_type}_metadata
- 此筛选器也位于wp-includes/meta.php
. 返回非null值将使update_metadata
函数,由调用update_post_meta
.add_{$meta_type}_metadata
- 实际上与相同update_{$meta_type}_metadata
, 除非有人要求add_post_meta
.delete_{$meta_type}_metadata
- 除用于删除元数据外,与上述所有操作相同。
使用这四个过滤器,您可以创建到另一个数据集的自定义连接器,而不必使用
wp_postmeta
桌子
<?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\' );
我强烈建议您正确地“屏蔽”您设置的功能,这意味着您应该只从远程数据库获取和发送您需要的内容,并允许WordPress处理剩余的Posteta。像这样的系统可能会产生一些不好的东西,所以要为头痛和调试会话做好准备。