从wp_insert_post

时间:2016-03-14 23:18:11

标签: wordpress custom-fields advanced-custom-fields

我在管理高级自定义字段时遇到问题(我有4个)。我想在添加新帖子时检查其中一些是否为空。我试过像空($ _ POST ['name_of_post_meta'])但它不起作用。 我怎么能抓到

function wpse120996_add_custom_field_automatically($post_id)
    {
        global $wpdb;

        if (!wp_is_post_revision($post_id)) {
            $category = get_the_category($post_id);
            $category = $category[0]->name;
            $link = get_permalink($post_id);

            if (($_POST['post_status'] == 'publish') && ($_POST['original_post_status'] != 'publish')) { // new post

                $lector = get_post_meta($post_id, 'lektor_pl', true);
                $subs_pl = get_post_meta($post_id, 'napisy_pl', true);
                $orginal = get_post_meta($post_id, 'wersja_eng', true);
                $subs_eng = get_post_meta($post_id, 'subs_eng', true);


                if (empty($_POST['lektor_pl'])) {
                    $lector = 0;
                }
                if (empty($_POST['napisy_pl'])) {
                    $subs_pl = 0;
                }
                if (empty($_POST['wersja_eng'])) {
                    $orginal = 0;
                }
                if (empty($_POST['subs_eng'])) {
                    $subs_eng = 0;
                }
                if (!empty($_POST['lektor_pl'])) {
                    $lector = 1;
                }
                if (!empty($_POST['napisy_pl'])) {
                    $subs_pl = 1;
                }
                if (!empty($_POST['wersja_eng'])) {
                    $orginal = 1;
                }
                if (!empty($_POST['subs_eng'])) {
                    $subs_eng = 1;
                }

                $sql = $wpdb->prepare("INSERT INTO `wp_cron_notification` (`id`, `post_id`, `subb_pl` , `lector`, `subb_eng`, `orginal`,`link`, `category`) values (NULL, %s, %s, %s, %s, %s, %s, %s)", $post_id, $subs_pl, $lector, $subs_eng, $orginal, $link, $category);
                $wpdb->query($sql) or die("ERROR #3");
            }
        }
    }

    add_action('wp_insert_post', 'wpse120996_add_custom_field_automatically', 1);

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以在这里使用ACF save_post函数(http://www.advancedcustomfields.com/resources/acfsave_post/),该函数在保存帖子后立即触发。点击此处了解更多信息:

如果您将以下内容放在functions.php文件中,您应该能够访问新保存的帖子并执行上面的代码。例如:

add_action('acf/save_post', 'post_save_update_title', 20);

function post_save_update_title($post_id){

    //Your code here

}