SO网友:Stephen M. Harris
奇怪的是,自v3以来,Wordpress提供了通过meta\\u id获取、更新和删除meta的功能。3,但从v3开始。7没有任何返回meta id的函数。因此,使用meta的核心函数。php作为参考,我实现了以下函数,以便能够检索元ID和值。
解决方案
使用下面的自定义功能
get_post_meta_db()
要获取元键ID和值,请使用Wordpress\'
update_meta()
和
delete_meta()
操纵post meta。
例如:
$meta = get_post_meta_db( $post_id, \'my_key\', true ); // Returns array(
// \'post_id\' => 5,
// \'meta_id\' = 33,
// \'meta_key\' => \'my_key\',
// \'meta_value\' => \'the_value_for_my_key\' );
update_meta( $meta[\'meta_id\'], $meta[\'meta_key\'], \'new_value\' );
以下是自定义函数定义:
/**
* Alternative to get_post_meta(), to retrieve meta_ids. @see get_meta_db()
*/
function get_post_meta_db( $post_id, $meta_key = null, $single = false, $meta_val = null, $output = OBJECT ){
return get_meta_db( \'post\', $post_id, $meta_key, $meta_val, $single, $output );
}
/**
* Alternative to get_metadata(). Differences:
* - returns every meta field (instead of only meta_values)
* - bypasses meta filters/actions
* - queries database, bypassing cache
* - returns raw meta_values (instead of unserializing arrays)
*
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Optional. Metadata key to retrieve. By default, returns all metadata for specified object.
* @param mixed $meta_val Optional. If specified, will only return rows with this meta_value.
* @param bool $single Optional. If true, returns single row, else returns array of rows.
* @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. @see wpdb::get_results()
*
* @return array Single metadata row, array of rows, empty array if no matches, or false if there was an error.
*/
function get_meta_db( $meta_type, $object_id = null, $meta_key = null, $meta_val = null, $single = false, $output = OBJECT ){
if( !$meta_type || !$table = _get_meta_table( $meta_type ) )
return false;
// Build query
global $wpdb;
$query = $wpdb->prepare( "SELECT * FROM $table", $object_id );
// Add passed conditions to query
$where = array();
if( $object_id = absint( $object_id ) )
$where[] = $wpdb->prepare( sanitize_key( $meta_type.\'_id\' ).\' = %d\', $object_id );
if( !empty($meta_key) )
$where[] = $wpdb->prepare( \'meta_key = %s\', wp_unslash( $meta_key ) );
if( null !== $meta_val )
$where[] = $wpdb->prepare( \'meta_value = %s\', maybe_serialize(wp_unslash($meta_val)));
if( !empty($where) )
$query .= \' WHERE \'.implode(\' AND \', $where );
if( $single )
$query .= \' LIMIT 1\';
$rows = $wpdb->get_results( $query, $output );
if( empty( $rows ) )
return ( $single ? null : array() );
/*
Uncomment this section to have this function unserialize values (like get_metadata())
NOTE: This can be implemented more efficiently using array_map
// Unserialize serialized meta_values
foreach( $rows as &$r ){
$v =& ($output==ARRAY_A ? $r[\'meta_value\'] : $output==ARRAY_N ? $r[3] : $r->meta_value );
$v = maybe_unserialize( $v );
} */
return ( $single ? reset( $rows ) : $rows );
}
注意:这些函数查询数据库,所以请谨慎使用此实现遵循中的约定
get_metadata_by_mid()
, 通过不使用
"get_post_metadata"
/
"get_user_metadata"
预筛选,不更新缓存在我的情况下,我只需要原始数据
get_metadata_by_mid()
, 此实现不会取消元值的序列化这些函数允许通过post\\u id/user\\u id、元键、元值或其任何组合来选择元数据