douyanti2808 2016-09-30 10: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 10: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部