I have a form that inserts a post into a custom post type with specific taxonomy category, like this:
$animal_name = sanitize_text_field($_POST['animal_name']); // Animal name
$animal_category = sanitize_text_field($_POST['animal_category']); // Dropdown list of animal categories, ID is passed
$args = array(
'post_title' => $animal_name,
'post_status' => 'publish',
'post_type' => 'animal'
);
$post_id = wp_insert_post( $args );
wp_set_object_terms( $post_id, intval( $animal_category ), 'species' );
What I can't figure out, is how to check if an animal name already exists in the 'animal' custom post type, in the 'species' taxonomy category (tag).
So as an example, if in the submitted form the $animal_name was 'Golden Retriever', and the $animal_category was 'Dog', the wp_insert_post() should not run if there is already a 'Golden Retriever' in the 'Dog' category.
WordPress already has a post_exists function, but it doesn't seem to cater for a taxonomy.
Aucun commentaire:
Enregistrer un commentaire