Home › Forums › General Issues › Unique custom field with Rest API
Hi,
I have a custom field that I use to store a publication ID from a third party app. Since it’s a unique ID in the third-party platform, I want to avoid having a duplicate on WordPress too. I added this code to my function.php, and it’s working well in the WordPress admin, but I need to do the same thing through a custom endpoint that we use to push content to avoid having duplicate publications.
Here’s the snippet in function.php:
add_filter('acf/validate_value/name='.'podboxx_id', 'acf_unique_value_field', 10, 4);
function acf_unique_value_field($valid, $value, $field, $input) {
if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} else {
$post_id = intval($_POST['post_id']);
}
if (!$post_id) {
return $valid;
}
$post_type = get_post_type($post_id);
$field_name = $field['name'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'podboxx_id',
'value' => $value
)
)
);
$query = new WP_Query($args);
if (count($query->posts)){
return 'This Value is not Unique. Please enter a unique '.$field['label'];
}
return true;
}
Here’s what I have right now as my custom endpoint:
public function api_endpoint_add_post($request) {
$form_data = $request->get_params();
$post_content = $request->get_param('post_content');
$post_title = $request->get_param('post_title');
$post_excerpt = $request->get_param('post_excerpt');
$url = $request->get_param('url');
$podboxx_id = $request->get_param('podboxx_id');
$titre_emission = $request->get_param('titre_emission');
$category = $request->get_param('category');
$post_tag = $request->get_param('post_tag');
$featured_image = $request->get_param('featured_image');
$post_content = $post_content ?? '';
$post_excerpt = $post_excerpt ?? '';
$new_categories = array();
if($featured_image){
$attach_id = $this->upload_image_from_url($featured_image);
}
if($category){
$categories = explode(',', $category);
foreach ($categories as $single_category) {
$term = term_exists( $single_category, 'category' );
if(!$term['term_id']){
$category_id = wp_insert_term($single_category, 'category');
$new_categories[] = $category_id;
}else{
$new_categories[] = $term['term_id'];
}
}
}
if($post_tag){
$post_tag = explode(',', $post_tag);
}
if ( ... something here ...){
$status = '200';
$message = 'Post with same content is already Exist.';
$data = '';
}else{
...
}
I want to adapt the code to make it filter duplicate posts through REST API. I’ve tried it but I’m not having success
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.