更新登录用户的帖子元数据

时间:2020-12-03 作者:sodanotpop

我已经创建了一个表单来收集我想要更新帖子的信息。我的代码工作得很好,但它没有为用户更新帖子,而是创建了一个新帖子。不知道我在更新当前帖子而不是创建新帖子时缺少了什么。以下是我使用的代码:


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 );
} 
}

1 个回复
最合适的回答,由SO网友:jdm2112 整理而成

欢迎使用WPSE。

需要注意的几个问题:

未定义$user\\u id,因此对wp\\u get\\u current\\u user()的调用将传递null值作为参数

$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_get_current_user/

    这一行$current_user->ID; 什么都不做。WP\\U用户类中没有ID方法。

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

$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;
关于更新后与创建,wp_update_post() 需要您正在更新的帖子的ID。您似乎将用户ID传递为$my_post[\'import_id\'] 这是无效的。

您是否正在尝试使用用户信息更新用户对象或帖子?

EDIT: OP wishes to update the meta key/value of each post for the current author.

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

注:update_post_meta() 需要post ID、meta\\u键(字段的slug)和要更新的每个字段的meta\\u值。https://developer.wordpress.org/reference/functions/update_post_meta/

// 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 );
}   
EDIT 为OP添加实际的元键和值细节。

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\' );
    }
}

相关推荐

Pre_User_Query与Pre_Get_Posts

我用过pre_user_query 编辑实际的sql查询,它工作得非常好。现在我想和pre_get_posts 这完全不是一回事。我需要做一个内部连接。。在这个函数中,它似乎只接受以下内容$query->set(\'meta_query\',$meta_query); 这太简单了。。。该设置不允许我添加联接子句,然后在WHERE子句中使用它。。这就是我需要做的。我是否使用了错误的函数?是否有pre_posts_query 而是存在的??其他人在这里干什么?任何指导都将不胜感激!