I make list of contacts using custom posts with meta fields. So I need to check meta_value ‘idcrm_contact_email’ for unique and if it is not unique don’t create new contact. I try to use ‘post_save’ – it’s doesn’t create any contacts. So I try to use ‘publish_post’ – it’s creates new contacts but doesn’t works for checking. This is my code:
add_action( 'publish_user_contact', 'contact_email_check', 1, 3 );
function contact_email_check( $post_ID ) {
// Check for mail duplicates.
if ( isset( $_POST['idcrm_contact_email'] ) ) {
$args = array(
'post_type' => 'user_contact',
'post_status' => 'publish',
'numberposts' => -1,
'meta_key' => 'idcrm_contact_email',
'meta_value' => $_POST['idcrm_contact_email'],
);
$check_contact_mail = count( get_posts( $args ) );
if ( $check_contact_mail > 0 ) {
wp_delete_post( $post_ID );
}
}
}
I use var_dump ($check_contact_mail) and get 1. So looks like check works but after I get a lot of errors:
Notice: Trying to get property ‘post_name’ of non-object in /wp-includes/post.php on line 6798 Notice: Trying to get property ‘post_date’ of non-object in /wp-includes/post.php on line 6841 Notice: Trying to get property ‘post_status’ of non-object in /wp-includes/post.php on line 6849 Notice: Trying to access array offset on value of type null in /includes/post.php on line 1065 Notice: Trying to access array offset on value of type null in /wp-admin/includes/post.php on line 1068 Warning: Cannot modify header information – headers already sent by (output started at /class-id-crm-contacts-user.php:578) in /wp-admin/post.php on line 231 Warning: Cannot modify header information – headers already sent by (output started at /class-id-crm-contacts-user.php:578) in /wp-includes/pluggable.php on line 1340 Warning: Cannot modify header information – headers already sent by (output started at /class-id-crm-contacts-user.php:578) in /wp-includes/pluggable.php on line 1343