Wordpress 将CPT id保存到当前用户元时发生致命错误

Wordpress 将CPT id保存到当前用户元时发生致命错误,wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,我有一个叫contrib的CPT。当我添加contrib post时,我想将contrib post的id插入到名为contrib-id的ACF(文本)中当前用户的用户元中 以下是我到目前为止对functions.php的介绍 知道如何修复这个致命错误吗 function save_contrib_id_to_user_meta($id, $post) { if($post->post_type != 'contrib') { return;

我有一个叫contrib的CPT。当我添加contrib post时,我想将contrib post的id插入到名为contrib-id的ACF(文本)中当前用户的用户元中

以下是我到目前为止对functions.php的介绍

知道如何修复这个致命错误吗

function save_contrib_id_to_user_meta($id, $post)
    {
        if($post->post_type != 'contrib') {
            return;
        }

    // update the current USER post 

    if ( is_user_logged_in() ) {
        update_user_meta( get_current_user_id(), 'contrib-id', ( $_POST['id'] ) );
    }
}    
add_action('save_post', 'save_contrib_id_to_user_meta');
这是它抛出的错误

Fatal error: Uncaught ArgumentCountError: Too few arguments to function save_contrib_id_to_user_meta(), 1 passed in /home/customer/www/mydomain.com/public_html/wp-includes/class-wp-hook.php on line 289 and exactly 2 expected in /home/customer/www/mydomain.com/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:1 Stack trace: #0 /home/customer/www/mydomain.com/public_html/wp-includes/class-wp-hook.php(289): save_contrib_id_to_user_meta(28505) #1 /home/customer/www/mydomain.com/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #2 /home/customer/www/mydomain.com/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #3 /home/customer/www/mydomain.com/public_html/wp-includes/post.php(4153): do_action('save_post', 28505, Object(WP_Post), true) #4 /home/customer/www/mydomain.com/public_html/wp-includes/post.php(4244): wp_insert_post(Array, false) #5 /home/customer/www/mydomain.com/public_html/wp-includes/post.php(3160): wp_update_post(Array) #6 /home/customer/www/r in /home/customer/www/mydomain.com/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 1

问题在于
add\u操作中,如果参数不止一个,则需要指定参数的数量(因为您正在传递
$id
$post

另外,您可以使用
save_post{post type}
hook,但是
save_post
仍然可以工作

function save_contrib_id_to_user_meta($id, $post)
    {
        if($post->post_type != 'contrib') {
            return;
        }

    // update the current USER post 

    if ( is_user_logged_in() ) {
        update_user_meta( get_current_user_id(), 'contrib-id', $id ) );
    }
}
// This is the line that needs to change. Adding the argument count.    
add_action('save_post', 'save_contrib_id_to_user_meta', 10, 2);

// You could use this instead and skip the post type check
add_action('save_post_contrib', 'save_contrib_id_to_user_meta', 10, 2);

您在尝试更新之前创建了post meta吗?我接受了您对save_post_contrib的建议,并查找了add_操作的引用,该操作的优先级似乎应该在参数计数之前。所以我使用了:add_action('save_post_contributors','save_contrib_id_to_user_meta',10,2);它不会抛出错误,但不会更新用户元。我想知道我是否犯了$post和/或$U post的错误!是的,我有优先权和论点。我不确定您想做什么,但是如果只是将post id添加到
contrib id
元字段中,只需使用
$id
,您已经在使用它传递post id了。我更新了答案。另外,请确保您的元字段实际上是
contrib id
,而不是
contrib_id
,因为默认情况下,ACF会用下划线标记它。