douyanti2808 2016-09-30 18:29
浏览 50
已采纳

如果条件为wp_insert_post_data,则Wordpress会阻止保存帖子

I have a custom plugin that uses custom fields to create a post ( these custom fields will be saved as metas ).

The problem is when these custom meta values are exactly the same for another post. The task is to throw an error message and not to save the post. So far i use wp_insert_post_data to trigger my function that checks if this meta values combination already exists for antoher post.

add_filter( 'wp_insert_post_data' , 'check_duplicate_relation' , '99', 2 );

My question is: how do i stop the post from being created? I tried to return array(), return false, but the post is still created with minimal required values. I know that my function should return altered data but what i tried is to somehow trigger an error message and the post would not be created.

Any ideea how i can do this?...or what hook should i use? As far as i searched this is the one that should be used.

Thanks.

UPDATE: Here is my function: Where and what i should do?

function check_duplicate_relation($data , $postData ) {

if($data['post_type'] == 'bpp_relation' && $data['post_status'] != 'auto-draft') {
    if(isset($postData['fields']) && !empty($postData['fields'])) {
        $bonusProgramId = intval(array_values($postData['fields'])[0]);
        $companyId = intval(array_values($postData['fields'])[1]);

        if($bonusProgramId && $bonusProgramId > 1 && $companyId && $companyId > 1) {
            $args = array(
                'posts_per_page' => 2, 
                'post_type' => 'bpp_relation',
                'meta_query' => array(
                     array(
                           'key' => 'company',
                           'value' => $companyId,
                           'compare' => '=',
                     ),
                     array(
                           'key' => 'bonus_program',
                           'value' => $bonusProgramId,
                           'compare' => '=',
                     )
                )
            );

            $relation = get_posts( $args );

            if(empty($relation)) {
                return $data;
            } else {
                // here the error should be returned
            }
        }

    }
}

return $data;

}

  • 写回答

1条回答 默认 最新

  • douanye8442 2016-09-30 18:38
    关注

    See the filter wp_insert_post_empty_content. If you return a value that evaluates true, the post will be prevented from being saved. See in WordPress source.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀