首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新已登录用户的Post Meta

更新已登录用户的Post Meta
EN

WordPress Development用户
提问于 2020-12-03 14:29:26
回答 1查看 281关注 0票数 1

我已经创建了一个收集信息的表单,我想用它更新一个帖子。我的代码很好用,但是它不是为用户更新一个帖子,而是创建一个新的帖子。不知道我错过了什么,以使当前的帖子更新,而不是创建一个新的帖子。下面是我使用的代码:

代码语言:javascript
复制
function update_role_and_post($record,$ajax_handler)
{
    $form_name = $record->get_form_settings('form_name');
    if ('Confirm Premium Account' !== $form_name) {
        return;
    }
    
$current_user = wp_get_current_user(); 
$current_user->remove_role( 'subscriber' );
$current_user->add_role( 'editor' );
    $form_data = $record->get_formatted_data();
    $update=$form_data["Content"]; 
    
    
    
// EVERYTHING UP TO THIS POINT WORKS
// EVERYTHING BELOW DOES NOT WORK!
    
    
// Assign the ID from the current WP_User object to a var for use below
$user_id = $current_user->ID;

    
$confirm=array( 
    'subscription'=> $subscription  // Wanting to use what is in the form and place it as the value for the upgraded meta-key
);
    
    
// Create array of arguments for our query
$args = array(
    'author' => $user_id,  // defined above
    'post_status' => 'publish',  // ignore deleted/drafted posts
    'fields' => 'ids',  // we only need the IDs
    'post_type'     => 'post',
    'meta_input'      => $confirm
);

// Query for posts by this author
$author_posts = new WP_Query( $args );

// WP_Query object contains lots of info; we only want the posts which will be an array if IDs based on our fields argument above.
$author_post_ids = $author_posts->posts;  // array of IDs only

// Loop through each post by this author
foreach ( $author_post_ids as $post_id ) {
    // On each loop iteration, update the post meta keys here
    update_post_meta( $post_id, $meta_key, $meta_value );
} 
}
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2020-12-03 17:13:46

欢迎来到WPSE。

需要注意的几个问题:

  1. 没有定义$user_id,因此对wp_get_current_user()的调用将空值作为参数传递。

$current_user = wp_get_current_user($user_id);. // $user_id is not defined

  1. wp_get_current_user()不接受参数。参见这里的文档:https://developer.wordpress.org/reference/functions/wp_到达_当前_用户/
  2. 这一行$current_user->ID;什么也不做。WP_User类中没有ID方法。

您可以将该部分简化如下:

代码语言:javascript
复制
$current_user = wp_get_current_user(); 
$current_user->remove_role( 'subscriber' );
$current_user->add_role( 'editor' );

// Assign the ID from the current WP_User object to a var for use below
$user_id = $current_user->ID;

关于post update和creation,wp_update_post()希望为您正在更新的帖子提供一个ID。您似乎将用户ID传递为无效的$my_post['import_id']

您是否试图用用户信息更新用户对象或帖子?

EDIT: OP希望更新当前作者的每个帖子的元键/值。

上面的代码解决了用户角色的挑战,下面是一个用于更新特定用户编写的每个帖子的post元的模型。

注意:update_post_meta()需要post ID、meta_key (字段的段塞)和正在更新的每个字段的meta_value。https://developer.wordpress.org/reference/functions/update_帖子_元/

代码语言:javascript
复制
// Create array of arguments for our query
$args = array(
    'author' => $user_id,  // defined above
    'post_status' => 'publish',  // ignore deleted/drafted posts
    'fields' => 'ids'  // we only need the IDs
);

// Query for posts by this author
$author_posts = new WP_Query( $args );

// WP_Query object contains lots of info; we only want the posts which will be an array if IDs based on our fields argument above.
$author_post_ids = $author_posts->posts;  // array of IDs only

// Loop through each post by this author
foreach ( $author_post_ids as $post_id ) {
    // On each loop iteration, update the post meta keys here
    update_post_meta( $post_id, $meta_key, $meta_value );
}   

<#>编辑为OP添加实际的元键和值细节。

代码语言:javascript
复制
function update_role_and_post( $record ) {

    if ( 'Confirm Premium Account' !== $record->get_form_settings( 'form_name' ) ) {
        return;
    }

    // Update current user role to editor
    $current_user = wp_get_current_user(); 
    $current_user->remove_role( 'subscriber' );
    $current_user->add_role( 'editor' );
    
    // Create array of arguments for our query
    $args = array(
        'author' => $current_user->ID, 
        'post_status' => 'publish', 
        'fields' => 'ids' 
    );
    
    // Query for posts by this author
    $author_posts = new WP_Query( $args );
    
    // Parse the post IDs from the rest of the WP_Query object
    $author_post_ids = $author_posts->posts;
    
    // Loop through each post by this author and update to premium
    foreach ( $author_post_ids as $post_id ) {
        update_post_meta( $post_id, 'subscription', 'premium' );
    }
}
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/379260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档