WordPress:保存时将用户元数据保存到 post 元数据
WordPress: Save user meta to post meta on save
我想在保存后向 post(在我的例子中是 WooCommerce 产品)添加一些用户元数据。我猜这是最好的方法?
我找到了一个解决方案,它做的事情是这样的:https://wordpress.stackexchange.com/a/273271/96806
但好像只做了一次?
代码如下:
function update_post_meta_with_user_meta() {
//setup arguments to only get users with author role
$user_args = array(
'role' => 'author',
);
$authors = get_users( $user_args );
//the below foreach could be replaced by adding something like:
// fields => array( 'ID' ) to $user_args
//instead I am just going through returned array of WP_User objects
// and putting IDs into array
foreach ( $authors as $a ) {
$author_ids[] = $a->ID;
}
//setup post query arguments to only give us posts with authors in author_id array
//which means only posts that have an author with the WP role of author
// should exclude Editors, Admins, etc. that maybe have authored posts
$post_args = array(
'author__in' => $author_ids,
);
//a new WP_Query with these args
$post_query = new WP_Query( $post_args ) ) );
//make sure we have posts returned
if ( $post_query->have_posts() ) {
//loop
while ( $post_query->have_posts() ) {
$post_query->the_post();
//set $post_id variable to current post
$post_id = get_the_id();
//get author meta for author of current post
$author_genere = get_the_author_meta('genere');
//update the meta of the current post (by ID)
// with the value of its author's user meta key
update_post_meta( $post_id, 'genere', $author_genere );
}
//reset the postdata
wp_reset_postdata();
}
}
//hook the above to init
add_action( 'init', 'update_post_meta_with_user_meta' );
保存 post/product 时有什么办法吗?
我想我找到了答案:
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
$vendor_id = $post->post_author;
$vendor_info = get_userdata($vendor_id);
$vendor_name = get_user_meta( $vendor_id, 'pv_shop_name', true );
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_vendor_name',
'placeholder' => $vendor_name,
'value' => $vendor_name,
'label' => __('Vendor', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_vendor_name = $_POST['_custom_product_vendor_name'];
if (!empty($woocommerce_custom_product_vendor_name))
update_post_meta($post_id, '_custom_product_vendor_name', esc_attr($woocommerce_custom_product_vendor_name));
}
函数获取用户的meta字段,保存后存入数据库
虽然您的答案可能与 'workaround' 一样,但它包含一些不必要的步骤,并且有一个更合适的钩子。
woocommerce_admin_process_product_object
- 在后端保存产品时保存产品元数据:
function action_woocommerce_admin_process_product_object( $product ) {
$vendor_id = $product->post_author;
$vendor_info = get_userdata( $vendor_id );
$vendor_name = get_user_meta( $vendor_id, 'pv_shop_name', true );
// For testing purposes, delete afterwards
$vendor_name = 'the_vendor_name';
$product->update_meta_data( '_custom_product_vendor_name', $vendor_name );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
我想在保存后向 post(在我的例子中是 WooCommerce 产品)添加一些用户元数据。我猜这是最好的方法?
我找到了一个解决方案,它做的事情是这样的:https://wordpress.stackexchange.com/a/273271/96806
但好像只做了一次?
代码如下:
function update_post_meta_with_user_meta() {
//setup arguments to only get users with author role
$user_args = array(
'role' => 'author',
);
$authors = get_users( $user_args );
//the below foreach could be replaced by adding something like:
// fields => array( 'ID' ) to $user_args
//instead I am just going through returned array of WP_User objects
// and putting IDs into array
foreach ( $authors as $a ) {
$author_ids[] = $a->ID;
}
//setup post query arguments to only give us posts with authors in author_id array
//which means only posts that have an author with the WP role of author
// should exclude Editors, Admins, etc. that maybe have authored posts
$post_args = array(
'author__in' => $author_ids,
);
//a new WP_Query with these args
$post_query = new WP_Query( $post_args ) ) );
//make sure we have posts returned
if ( $post_query->have_posts() ) {
//loop
while ( $post_query->have_posts() ) {
$post_query->the_post();
//set $post_id variable to current post
$post_id = get_the_id();
//get author meta for author of current post
$author_genere = get_the_author_meta('genere');
//update the meta of the current post (by ID)
// with the value of its author's user meta key
update_post_meta( $post_id, 'genere', $author_genere );
}
//reset the postdata
wp_reset_postdata();
}
}
//hook the above to init
add_action( 'init', 'update_post_meta_with_user_meta' );
保存 post/product 时有什么办法吗?
我想我找到了答案:
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
$vendor_id = $post->post_author;
$vendor_info = get_userdata($vendor_id);
$vendor_name = get_user_meta( $vendor_id, 'pv_shop_name', true );
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_vendor_name',
'placeholder' => $vendor_name,
'value' => $vendor_name,
'label' => __('Vendor', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_vendor_name = $_POST['_custom_product_vendor_name'];
if (!empty($woocommerce_custom_product_vendor_name))
update_post_meta($post_id, '_custom_product_vendor_name', esc_attr($woocommerce_custom_product_vendor_name));
}
函数获取用户的meta字段,保存后存入数据库
虽然您的答案可能与 'workaround' 一样,但它包含一些不必要的步骤,并且有一个更合适的钩子。
woocommerce_admin_process_product_object
- 在后端保存产品时保存产品元数据:
function action_woocommerce_admin_process_product_object( $product ) {
$vendor_id = $product->post_author;
$vendor_info = get_userdata( $vendor_id );
$vendor_name = get_user_meta( $vendor_id, 'pv_shop_name', true );
// For testing purposes, delete afterwards
$vendor_name = 'the_vendor_name';
$product->update_meta_data( '_custom_product_vendor_name', $vendor_name );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );