将 CPT id 保存到当前用户元的致命错误
Fatal error saving CPT id to current user meta
我有一个名为 contrib 的 CPT。当我添加一个贡献 post 时,我想将贡献 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_action
,如果参数多于一个,您需要指定参数的数量(因为您传递的是 $id
和 $post
)。
此外,您可以使用 save_post_{post-type}
挂钩,但 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);
我有一个名为 contrib 的 CPT。当我添加一个贡献 post 时,我想将贡献 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_action
,如果参数多于一个,您需要指定参数的数量(因为您传递的是 $id
和 $post
)。
此外,您可以使用 save_post_{post-type}
挂钩,但 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);