Julie
Julie

Reputation: 17

Adding acf new field causing error with my exisitng function

I'm trying to create new field using ACF but my existing code is causing problem, Please have a look at the code first add_filter('wp_insert_post_data', 'makeNotePrivate', 10, 2);

function makeNotePrivate($data, $postarr)
{

  if((count_user_posts(get_current_user_id(), 'note') > 4) AND !$postarr['ID']) //I'm defining restriction that user not able to create more than 4 note(Note is custom post type)
  {
    die("You have reached your limit ");  
  }
  if($data['post_type'] == 'note')
  {
    $data['post_title'] = sanitize_text_field($data['post_title']);
    $data['post_content'] = sanitize_textarea_field($data['post_content']);
  }
  if($data['post_type'] = 'note' AND $data['post_status']!='trash')
  {
    $data['post_status'] = "private";
  }
  return $data;
}

Now Problem is that when I creating new field in Advance custom plugin than It shows "You have reached your limit". It doesn't make any sense because I've define the condition for Note type.

Upvotes: 0

Views: 76

Answers (1)

ZealousWeb
ZealousWeb

Reputation: 1751

Your is checking not checking for the post type at the first condition. You can use below condition to check the post type:

function makeNotePrivate($data, $postarr)
{

  if((count_user_posts(get_current_user_id(), 'note') > 4) && !$postarr['ID'] && $data['post_type'] == 'note') //I'm defining restriction that user not able to create more than 4 note(Note is custom post type)
  {
    die("You have reached your limit ");  
  }
  if($data['post_type'] == 'note')
  {
    $data['post_title'] = sanitize_text_field($data['post_title']);
    $data['post_content'] = sanitize_textarea_field($data['post_content']);
  }
  if($data['post_type'] = 'note' && $data['post_status']!='trash')
  {
    $data['post_status'] = "private";
  }
  return $data;
}

Upvotes: 1

Related Questions