Forum Replies Created

Viewing 1 replies (of 1 total)
  • I am using wp_insert_post_data() function to publish posts that are in pending post status. what i want to accomplish is increment post title on post publish. I figure it out that wp_insert_post_data will insert post data on post save and update. My problem is i want to publish post with increment post title but not on post update.

    add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );  
    function modify_post_title( $data , $postarr ) {
        
      // Check for the custom post type and it's status
        // We only need to modify it when it's going to be published
        
        $posts_status = ['publish'];
        if( $data['post_type'] == 'matrimony' && in_array($data['post_status'], $posts_status)) {
            
            $count_posts = wp_count_posts('matrimony');
        	$published_posts = $count_posts->publish;
        	$pending_posts = $count_posts->pending;
          	if ($published_posts == 0) {
                if ($pending_posts == 0) {
                    $data['post_title'] = 'ACMB' . '-1';
    		        $data['post_name'] = sanitize_title($data['post_title']);
                }
                else{
                    // Get the most recent post
                    $newposts = array(
                        'numberposts' => 1,
                        'orderby' => 'post_date',
                        'order' => 'DESC',
                        'post_type' => 'matrimony',
                        'post_status' => 'publish'
                    );
           
                    $last_post = wp_get_recent_posts($newposts);
                    $last__post_title = $last_post['0']['post_title'];
          	        $number = preg_split('/[^[:alnum:]]+/', $last__post_title);
                    $number = $number[1] + 1;
                    
                    // Save the title.
                    $data['post_title'] = 'ACMB' . '-' . $number;
    		        $data['post_name'] = sanitize_title($data['post_title']);
                }
        	} 
        	else {
    
            // Get the most recent post
            $newposts = array(
                        'numberposts' => 1,
                        'orderby' => 'post_date',
                        'order' => 'DESC',
                        'post_type' => 'matrimony',
                        'post_status' => 'publish'
                    );
           
            $last_post = wp_get_recent_posts($newposts);
            $last__post_title = $last_post['0']['post_title'];
          	$number = preg_split('/[^[:alnum:]]+/', $last__post_title);
            $number = $number[1] + 1;
            
            // Save the title.
            $data['post_title'] = 'ACMB' . '-' . $number;
    	    $data['post_name'] = sanitize_title($data['post_title']);
            
            
        }
        }
        return $data;
    }

    Above code will count posts from custom post type and find latest post by date created and increment the post title like ACMB-1,2,3…so on
    My problem is this function fires on update post which i dont want

Viewing 1 replies (of 1 total)