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 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题