简体   繁体   中英

Auto Increment Custom Post Type Title

I created a custom post type "Testimonials" and removed support for the title. I am wanting to auto increment the title such as Testimony #1, Testimony #2, Testimony #3, etc.. right now it saves as "auto draft". Any help would be appreciated.

BTW I am not echoing the title, it will only be visible by me.

Made some improvements to the code provided @the-alpha.

// Use a filter to make the change
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
function modify_post_title( $data , $postarr ) {

// Check for the custom post type and if it's in trash
// We only need to modify if it when it's going to be published
$posts_status = ['publish', 'future', 'draft', 'pending', 'private', 'trash'];
if( $data['post_type'] == 'oferta' && !in_array($data['post_status'], $posts_status)) {

    // Count the number of posts to check if the current post is the first one
    $count_posts = wp_count_posts('you custom post type');
    $published_posts = $count_posts->publish;

    // Check if it's the first one
    if ($published_posts == 0) {

        $data['post_title'] = date('Y-m') . '-1';

    } else {

        // Get the most recent post
        $args = array(
            'numberposts' => 1,
            'orderby' => 'post_date',
            'order' => 'DESC',
            'post_type' => 'you custom post type',
            'post_status' => 'publish'
        );
        $last_post = wp_get_recent_posts($args);
        // Get the title
        $last_post_title = $last_post['0']['post_title'];
        // Get the title and get the number from it.
        // We increment from that number
        $number = explode('-', $last_post_title);
        $number = $number[2] + 1;

        // Save the title.
        $data['post_title'] = date('Y-m') . '-' . $number;

    }        
}
return $data;
}

In this case the user doesn't have the ability to modify the custom post type title and whole thing increments based on the title.

You may try this

add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
function modify_post_title( $data , $postarr )
{
    if( $data['post_type'] == 'your custom post type' ) {
        $last_post = wp_get_recent_posts( '1');
        $last__post_id = (int)$last_post['0']['ID'];
        $data['post_title'] = 'Testimony #' . ($last__post_id+1);
    }
    return $data;
}

Just paste this code in your functions.php file.

on post submission from frontend custom post title set to #1 but not increment to #2 on second post submission..

This is the exact code used in functions.php

add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
function modify_post_title( $data , $postarr )
   {
if( $data['post_type'] == 'your custom post type' ) {
    $last_post = wp_get_recent_posts( '1');
    $last__post_id = (int)$last_post['0']['ID'];
    $data['post_title'] = 'Testimony #' . ($last__post_id+1);
}
return $data;
}

I use wp_insert_post_data to modify post title but my problem is post title modified on post publish but it also modify again if post is updated on post update.

My code

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;
}

How to call the above function only on post publish

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM