Php 如何将帖子的ID保存到元数据?

Php 如何将帖子的ID保存到元数据?,php,wordpress,Php,Wordpress,我正在使用Wordpress的工具集插件,并试图让它保存用户填写表单的页面id 这个插件有“挂钩” 这就是他们所说的 cred\u save\u数据 描述 This hook allows doing a custom action when post data is saved to the database. 论据 post_id. The id of the current created or edited post. form_data. An associativ

我正在使用Wordpress的工具集插件,并试图让它保存用户填写表单的页面id

这个插件有“挂钩”

这就是他们所说的

cred\u save\u数据

描述

This hook allows doing a custom action when post data is saved to the database.
论据

    post_id. The id of the current created or edited post.

    form_data. An associative array of data about current form:
        id. The form of ID.
        post_type. The post type that the form operates on.
        form_type. The type of form - 'new' or 'edit'.
        container_id. Refers to the post_id of the post, page or custom post type that contains the CRED form
例如:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        if (isset($_POST['my_custom_field']))
        {
            // add it to saved post meta
            add_post_meta($post_id, '__my_custom_field', $_POST['my_custom_field'], true);
        }
    }
}
因此,我在主题的functions.PHP中添加了修改后的PHP代码:


add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2080) // the id of the form
    {
        if (isset($_POST['container_id']))
        {
            // add it to saved post meta
            add_post_meta($post_id, 'formetadata', $_POST['container_id'], true);
        }
    }
}
其中for metadata是一个slug for自定义字段,用于收集元数据,包括填写表单的页面id

它不起作用。表单收集数据,创建帖子,但元数据字段保持为空

我做错了什么


我没有足够的PHP知识,可能完全被误解了说明…

因此,查看文档和您的函数,我发现它们使用了键
container\u id
,而您使用的是
$\u POST['container\u id']
。我只是在猜测,但听起来它们是相同的
id
?如果是这样,您可以通过
$form\u data
访问它,如下所示:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2080) // the id of the form
    {
        if (isset($_POST['container_id']))
        {
            // add it to saved post meta
            add_post_meta($post_id, 'formetadata', $form_data['container_id'], true);
        }
    }
}

因此,查看文档和您的函数,我看到它们使用键
container\u id
,您使用
$\u POST['container\u id']
。我只是在猜测,但听起来它们是相同的
id
?如果是这样,您可以通过
$form\u data
访问它,如下所示:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2080) // the id of the form
    {
        if (isset($_POST['container_id']))
        {
            // add it to saved post meta
            add_post_meta($post_id, 'formetadata', $form_data['container_id'], true);
        }
    }
}

我认为第一个条件是错误的。表单中的所有内容都是字符串。因此,
$form_data['id']==“2080”)
应该可以使用堆栈溢出。您所展示的代码没有明显的问题,因此我们没有什么可以继续的。您需要调试它以跟踪问题发生的位置。第一件事是确保函数被调用。如果是,请检查值是否符合预期。在
my\u save\u data\u action
函数中添加以下代码作为第一件事:
var\u dump($form\u data);回显“my_custom_field in POST:”.$\u POST['my_custom_field']。”

”;die()
并让我们知道发生了什么。我认为第一个条件是错误的。表单中的所有内容都是字符串。因此,
$form_data['id']==“2080”)
应该可以使用堆栈溢出。您所展示的代码没有明显的问题,因此我们没有什么可以继续的。您需要调试它以跟踪问题发生的位置。第一件事是确保函数被调用。如果是,请检查值是否符合预期。在
my\u save\u data\u action
函数中添加以下代码作为第一件事:
var\u dump($form\u data);回显“my_custom_field in POST:”.$\u POST['my_custom_field']。”

”;die()
并让我们知道发生了什么。