来自自定义发布类型的高级自定义字段发布字段

时间:2013-10-08 作者:bestprogrammerintheworld

我正在使用高级自定义字段插件,我有一个名为“course”的自定义帖子类型,其中一些来自ACF的字段与之相关。

我有以下代码:

function update_course($course_id) {

    //Check conditions and tell user if certain values are incorrect/inconsitence
    $nr_available = intval(get_field(\'nr_available\'));
    $nr_registered = intval(get_field(\'nr_registered\'));

    echo \'nr avail=\' . $nr_available;
    echo \'nr registered=\' . $nr_registered;

}

add_action( \'edit_post\' , \'update_course\');
我想做的事情是检查可用点是否少于,然后注册=不正确。然后我想指出用户做得不正确。

但无论如何,我真正的问题是,我不知道如何(我不知道)从发布的自定义帖子类型的ACF中获取值(当然)。

上面只会返回数据库中的实际值(我认为对于当前课程),I want the posted values. 如果我查看$\\u POST变量,它会显示如下内容:

[field_524bceba787b8] => 800 [field_524bcf0a787b9] => 40 [field_524bcf58787ba] => 50 [field_524bcfdb787bb] => [field_524bd005787bc] => [field_5253d4b31053b] => 0
UPDATE:整个阵列看起来像:

Array ( [_wpnonce] => 8fce853ce2 [_wp_http_referer] => /wp-admin/post.php?post=1911&action=edit&message=1 [user_ID] => 1 [action] => editpost [originalaction] => editpost [post_author] => 1 [post_type] => course [original_post_status] => publish [referredby] => http://{host}/wp-admin/post.php?post=1911&action=edit&message=1 [_wp_original_http_referer] => http://{host}/wp-admin/post.php?post=1911&action=edit&message=1 [post_ID] => 1911 [autosavenonce] => 0995694d61 [meta-box-order-nonce] => 1da8897fe1 [closedpostboxesnonce] => c92ea6dd81 [post_title] => Balett 8-9 år [samplepermalinknonce] => 31966e47cc [content] => coolt222 [wp-preview] => [hidden_post_status] => publish [post_status] => publish [hidden_post_password] => [hidden_post_visibility] => public [visibility] => public [post_password] => [mm] => 10 [jj] => 02 [aa] => 2013 [hh] => 07 [mn] => 52 [ss] => 46 [hidden_mm] => 10 [cur_mm] => 10 [hidden_jj] => 02 [cur_jj] => 08 [hidden_aa] => 2013 [cur_aa] => 2013 [hidden_hh] => 07 [cur_hh] => 20 [hidden_mn] => 52 [cur_mn] => 55 [original_publish] => Uppdatera [save] => Uppdatera [tax_input] => Array ( [categorycourses] => Array ( [0] => 0 [1] => 29 [2] => 27 ) ) [newcategorycourses] => Namn för ny kategori [newcategorycourses_parent] => -1 [_ajax_nonce-add-categorycourses] => 2a26873ebd [acf_nonce] => 52e8f939a6 [fields] => Array ( [field_524bcd27ed097] => [field_524bcde2787b4] => o [field_524bce12787b5] => 433 [field_524bce5f787b6] => 1/:00 [field_524bce98787b7] => [field_524bceba787b8] => 1000 [field_524bcf0a787b9] => 10000 [field_524bcf58787ba] => 5 [field_524bcfdb787bb] => [field_524bd005787bc] => [field_5253d4b31053b] => 0 ) [advanced_view] => 1 [add_comment_nonce] => 82940723b5 [_ajax_fetch_list_nonce] => 83bc001ed1 [post_name] => balett-8-9-ar [post_author_override] => 1 [post_mime_type] => [ID] => 1911 [post_content] => coolt222 [comment_status] => closed [ping_status] => closed ) 

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

您可以找到在中添加的高级自定义字段字段$_POST “”下的数组fields\' 钥匙使用ACFget_field_object() 函数获取所添加字段的键。

因此,如果您将字段命名为“nr\\u available”,则必须找到它的键才能在$\\u POST对象中找到该字段(这样才能获取$\\u POST[\'fieldKey\'))。

检查以下代码:

function update_course($course_id)
{
    $nr_available = 0;
    $nr_registered = 0;

    $field1 = get_field_object(\'nr_available\');
    $nr_available_key = $field1[\'key\'];

    $field2 = get_field_object(\'nr_registered\');
    $nr_registered_key = $field2[\'key\'];

    // loop the $_POST[\'fields\'] which contains all the Advanced Custom Fields fields which you added to the post you are editing
    foreach ($_POST[\'fields\'] as $k=>$v)
    {
        // $k are the custom fields keys
        // $v are the custom fields values

        if ($k == $nr_available_key){
            $nr_available = $v;
        }
        if ($k == $nr_registered_key){
            $nr_registered = $v;
        }
     }

     // Do your checks after you get the values
     if ( ($nr_available > $nr_registered) || $nr_available <= 0 )
     {
        // Bad
        return;
     }
}
add_action( \'edit_post\' , \'update_course\');

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在

来自自定义发布类型的高级自定义字段发布字段 - 小码农CODE - 行之有效找到问题解决它

来自自定义发布类型的高级自定义字段发布字段

时间:2013-10-08 作者:bestprogrammerintheworld

我正在使用高级自定义字段插件,我有一个名为“course”的自定义帖子类型,其中一些来自ACF的字段与之相关。

我有以下代码:

function update_course($course_id) {

    //Check conditions and tell user if certain values are incorrect/inconsitence
    $nr_available = intval(get_field(\'nr_available\'));
    $nr_registered = intval(get_field(\'nr_registered\'));

    echo \'nr avail=\' . $nr_available;
    echo \'nr registered=\' . $nr_registered;

}

add_action( \'edit_post\' , \'update_course\');
我想做的事情是检查可用点是否少于,然后注册=不正确。然后我想指出用户做得不正确。

但无论如何,我真正的问题是,我不知道如何(我不知道)从发布的自定义帖子类型的ACF中获取值(当然)。

上面只会返回数据库中的实际值(我认为对于当前课程),I want the posted values. 如果我查看$\\u POST变量,它会显示如下内容:

[field_524bceba787b8] => 800 [field_524bcf0a787b9] => 40 [field_524bcf58787ba] => 50 [field_524bcfdb787bb] => [field_524bd005787bc] => [field_5253d4b31053b] => 0
UPDATE:整个阵列看起来像:

Array ( [_wpnonce] => 8fce853ce2 [_wp_http_referer] => /wp-admin/post.php?post=1911&action=edit&message=1 [user_ID] => 1 [action] => editpost [originalaction] => editpost [post_author] => 1 [post_type] => course [original_post_status] => publish [referredby] => http://{host}/wp-admin/post.php?post=1911&action=edit&message=1 [_wp_original_http_referer] => http://{host}/wp-admin/post.php?post=1911&action=edit&message=1 [post_ID] => 1911 [autosavenonce] => 0995694d61 [meta-box-order-nonce] => 1da8897fe1 [closedpostboxesnonce] => c92ea6dd81 [post_title] => Balett 8-9 år [samplepermalinknonce] => 31966e47cc [content] => coolt222 [wp-preview] => [hidden_post_status] => publish [post_status] => publish [hidden_post_password] => [hidden_post_visibility] => public [visibility] => public [post_password] => [mm] => 10 [jj] => 02 [aa] => 2013 [hh] => 07 [mn] => 52 [ss] => 46 [hidden_mm] => 10 [cur_mm] => 10 [hidden_jj] => 02 [cur_jj] => 08 [hidden_aa] => 2013 [cur_aa] => 2013 [hidden_hh] => 07 [cur_hh] => 20 [hidden_mn] => 52 [cur_mn] => 55 [original_publish] => Uppdatera [save] => Uppdatera [tax_input] => Array ( [categorycourses] => Array ( [0] => 0 [1] => 29 [2] => 27 ) ) [newcategorycourses] => Namn för ny kategori [newcategorycourses_parent] => -1 [_ajax_nonce-add-categorycourses] => 2a26873ebd [acf_nonce] => 52e8f939a6 [fields] => Array ( [field_524bcd27ed097] => [field_524bcde2787b4] => o [field_524bce12787b5] => 433 [field_524bce5f787b6] => 1/:00 [field_524bce98787b7] => [field_524bceba787b8] => 1000 [field_524bcf0a787b9] => 10000 [field_524bcf58787ba] => 5 [field_524bcfdb787bb] => [field_524bd005787bc] => [field_5253d4b31053b] => 0 ) [advanced_view] => 1 [add_comment_nonce] => 82940723b5 [_ajax_fetch_list_nonce] => 83bc001ed1 [post_name] => balett-8-9-ar [post_author_override] => 1 [post_mime_type] => [ID] => 1911 [post_content] => coolt222 [comment_status] => closed [ping_status] => closed ) 

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

您可以找到在中添加的高级自定义字段字段$_POST “”下的数组fields\' 钥匙使用ACFget_field_object() 函数获取所添加字段的键。

因此,如果您将字段命名为“nr\\u available”,则必须找到它的键才能在$\\u POST对象中找到该字段(这样才能获取$\\u POST[\'fieldKey\'))。

检查以下代码:

function update_course($course_id)
{
    $nr_available = 0;
    $nr_registered = 0;

    $field1 = get_field_object(\'nr_available\');
    $nr_available_key = $field1[\'key\'];

    $field2 = get_field_object(\'nr_registered\');
    $nr_registered_key = $field2[\'key\'];

    // loop the $_POST[\'fields\'] which contains all the Advanced Custom Fields fields which you added to the post you are editing
    foreach ($_POST[\'fields\'] as $k=>$v)
    {
        // $k are the custom fields keys
        // $v are the custom fields values

        if ($k == $nr_available_key){
            $nr_available = $v;
        }
        if ($k == $nr_registered_key){
            $nr_registered = $v;
        }
     }

     // Do your checks after you get the values
     if ( ($nr_available > $nr_registered) || $nr_available <= 0 )
     {
        // Bad
        return;
     }
}
add_action( \'edit_post\' , \'update_course\');

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在