maxworkingwell

CRED check duplicates of current user by title

Jul 22nd, 2021 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. /* Validate CRED Forms check duplicates of current user by title */
  2.  
  3. add_filter('cred_form_validate','func_validate_title',10,2);
  4. function func_validate_title($error_fields, $form_data){
  5.      
  6.     list($fields,$errors)=$error_fields;
  7.      
  8.     $forms_array =  array( 48 );
  9.  
  10.     if (in_array($form_data['id'], $forms_array)) {
  11.  
  12.         // get current user and title
  13.         $current_user = wp_get_current_user();
  14.         $title = $_POST['post_title'];
  15.  
  16.         // query to get the posts to compare
  17.         $args = array( "author" => $current_user->ID, "post_type" => "add", "posts_per_page" => -1, "post_status" => array('publish', 'draft') );
  18.         $query = get_posts( $args );
  19.  
  20.         if (count($query) > 0){
  21.  
  22.             //set error message for the title field if a post of the same title exists
  23.             foreach ($query as $post) {
  24.                 if (strtolower($post->post_title) == strtolower($title)) {
  25.                     $errors['post_title']='Найден дубликат в Ваших объявлениях. Проверьте Личный кабинет > Объявления > Неактивные.';
  26.                     break;
  27.                 }
  28.             }
  29.         }
  30.     }
  31.  
  32.     //return result
  33.     return array($fields,$errors);
  34. }
Add Comment
Please, Sign In to add comment