您可以使用这样的过滤器来获取匹配的author元字段(使用post元键检查author元键值):
add_filter(\'get_post_metadata\', \'custom_get_author_meta\', 10, 4);
function custom_get_author_meta($metadata, $post_id, $meta_key, $single) {
if (!$metadata) {
$post = get_post($post_id);
$author_id = $post->post_author;
$value = get_the_author_meta($meta_key, $author_id);
if ($value) {return $value;}
}
return $metadata;
}
Update: 已编辑问题的具体答案:
add_filter(\'get_post_metadata\', \'custom_get_author_company_meta\', 10, 4);
function custom_get_author_meta($metadata, $post_id, $meta_key, $single) {
if (!$meta_key == \'company\') {
$post = get_post($post_id);
$author_id = $post->post_author;
$value = get_user_meta($author_id, \'company\', true);
if ($value) {return $value;}
}
return $metadata;
}
执行以下操作时,应从作者的用户元字段返回company字段:
$company = get_post_meta($postid, \'company\', true);
因此,如果您处于循环中,也可以执行以下操作:
global $post;
$company = get_post_meta($post->ID, \'company\', true);
UPDATE: 此替代解决方案将循环所有现有的post记录,并同步
post_meta
字段“company”(公司)
author_meta
相同姓名的帖子作者的密钥。。。每天通过WP Cron。
function author_company_post_sync() {
global $wpdb;
$posts= $wpdb->get_results("SELECT ID,post_author FROM ".$wpdb->prefix."posts");
foreach ($posts as $post) {
$company = get_user_meta($post->post_author, \'company\', true);
update_post_meta($post->ID, \'company\', $company);
}
}
// schedule with WP Cron to run sync callback once daily...
if (!wp_next_scheduled(\'author_company_post_sync\')) {
wp_schedule_event(time(), \'daily\', \'author_company_post_sync\');
}
这应该使它与任何想要
post_meta
指定的字段,因为它现在也将实际存在于该表中。:-)