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 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?