支票应转到wp_insert_post
. 每当发布或编辑帖子时,就会触发此挂钩。
在那里,您可以进行自定义查询以检查是否有任何帖子已经具有相同的内容xxxx_url
值或否。
add_action(\'wp_insert_post\', function($post_id) {
$meta_key = \'xxxx_url\';
$meta_value = get_post_meta($post_id, $meta_key, true);
$query = new WP_Query([
\'post_type\' => get_post_type($post_id), // This might be unnecessary, if you check `post` post type only. Or use `any`.
\'meta_query\' => [
[
\'meta_key\' => $meta_key,
\'meta_value\' => $meta_value,
]
]
]);
if ($query->have_posts()) {
// invalid key, post with the same value already exists
} else {
// valid, key was not found anywhere
}
});