Home Forums ACF PRO Check post type before publishing post


Check post type before publishing post

  • Hello
    i use the function below in order to generate a post title depending on its post type during its initial creation.
    The issue is that i don’t seem to be able to find a way to regenerate the title when new data are inserted or changed (in regards to the title that is being generated from acf field data),basically when a post is edited and saved and not during the initial creation of the post

    this is the function i am using:

    function cst_check_post_title( $data, $postarr ) {
        if ( is_array( $data ) && 'publish' == $data['post_status'] && empty( $data['post_title'] ) && 'parastatika' == $data['post_type']) {
            $cst_p_title ='Απόδειξη Παροχής Υπηρεσιών #';
            $data['post_title'] = $cst_p_title;
        }elseif ( is_array( $data ) && 'publish' == $data['post_status'] && 'pelatologio' == $data['post_type']) {
            $cst_t_date=date("d/m/Y", strtotime($_POST['acf']['field_5a6b3543bf97a']));
    		$cst_title =$_POST['acf']['field_5a6770a3c509d'].' '.mb_strimwidth($_POST['acf']['field_5a6b343038f0c'], 0, 3 ).' '.$_POST['acf']['field_5a5f76d271881'].' '.$cst_t_date;
            $data['post_title'] = $cst_title;
        return $data;
    add_filter( 'wp_insert_post_data', 'cst_check_post_title', 10, 2 );

    any suggestions?

  • If you are using ACF fields values to generate your value then you need to us the acf/save_post action instead of wp_insert_post_data. ACF does not save fields until after your filter runs so you are seeing the old values of any fields and not any changes.

    If you want to use the hook you’re using then you need to look in $_POST[‘acf’] to get the new values. This is covered on the above page as well.

  • I guess i need two seperate functions, one for the new posts and one for the posts being updated, i suppose i can keep my function as is and add a condition to check if the post is new and a seperate function that uses the acf/save_post and run this one for published post.

    Does this sound correct to you?

    Thanks for your assistance!

  • I can’t tell by looking at your code exactly what you’re trying to do, but if all you are doing is setting the post title based on the post type and using values from acf fields and maybe other values, then I would probably use just one acf/save_post filter. ACF passes the post ID and you can use this to get whatever you need.

    add_action('acf/save_post', 'your_function_name_here', 20);
    function your_function_name_here($post_id) {
      if (!is_nemeric($post_id)) {
        // post id is not numeric
        // it is probably for options, user or term
        // no need to go further
      $post_type = get_post_type($post_id);
      // do something based on $post_type;
  • I tried this function but it doesn’t save the title when i create a new post of post type “pelatologio” but it does save the title if i open the new post and save it again

    add_action('acf/save_post', 'cst_check_post_title', 20);
    function cst_check_post_title($post_id) {
      if (!is_numeric($post_id)) {
      $post_type = get_post_type($post_id);
      if ($post_type == 'pelatologio'){
      		$cst_t_date=date("d/m/Y", strtotime($_POST['acf']['field_5a6b3543bf97a']));
    		$cst_epi = get_field('epitheto');
    		$cst_pat = get_field('onoma_patros');
    		$cst_ono = get_field('onoma');
    		$cst_title =$cst_epi.' '.mb_strimwidth($cst_pat, 0, 3 ).' '.$cst_ono.' '.$cst_t_date;
    	  	$data['post_title'] = $cst_title;
      }else if ($post_type == 'parastatika'){
      		$data['post_title'] = 'Απόδειξη Παροχής Υπηρεσιών #';
      wp_update_post( $data );

    i tried using the $_POST[‘acf’] way as well but that one doesn’t seem to work either with the acf/save_post for the initial save i have considered using acf/save_post with $_POST[‘acf’] and lower the priority to 10 and see if that would work for both New Posts and Updated Posts

  • You need to supply the post ID of the post you want to update in $data

    $data['ID'] = $post_id;

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Check post type before publishing post’ is closed to new replies.