最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin gravity forms - Dynamically add a unique numberidentifier at the end of post titles

programmeradmin0浏览0评论

I am currently developing a small recruitment style website, and have encountered an issue.

I have created a form (Using Gravity Forms) that allows users to create a custom post (Current Vacancies) and have a hidden field that pulls the post title from one of the other fields dynamically.

However because there will be many posts with an almost identical post title I want to dynamically add a way to identify them through something like NTS0001 Etc. adding an additional number for every new post created.

I use multiple post types to help organise each section of my website and I only want this dynamic number/identifier on to a sing custom post type (called 'current_vacancies'

P.S If possible I would also like this to show in the URL of the post as well. I'm aware Wordpress has the numeric permalink setting however will that always say the same as the post title?

Hopefully this makes sense.

I am currently developing a small recruitment style website, and have encountered an issue.

I have created a form (Using Gravity Forms) that allows users to create a custom post (Current Vacancies) and have a hidden field that pulls the post title from one of the other fields dynamically.

However because there will be many posts with an almost identical post title I want to dynamically add a way to identify them through something like NTS0001 Etc. adding an additional number for every new post created.

I use multiple post types to help organise each section of my website and I only want this dynamic number/identifier on to a sing custom post type (called 'current_vacancies'

P.S If possible I would also like this to show in the URL of the post as well. I'm aware Wordpress has the numeric permalink setting however will that always say the same as the post title?

Hopefully this makes sense.

Share Improve this question asked Mar 5, 2019 at 10:15 RazeyRazey 31 silver badge4 bronze badges 1
  • You would need this filter: developer.wordpress/reference/hooks/wp_insert_post_data as used e.g. here. You could read the suffix from wp_options, add 1 to it and store it back, unless there is a reasonable chance of two posts being saved at the same time. – Jos Commented Mar 5, 2019 at 13:28
Add a comment  | 

2 Answers 2

Reset to default 0

You will need to leverage the post_updated hook API of Wordpress

// You will need to leverage the <code>save post</code> hook API of Wordpress
add_action( 'post_updated', 'save_post_callback', 10, 3 );

//add_action('save_post', 'save_post_callback');
function save_post_callback($post_ID, $post_after, $post_before){
    global $post;
    if ($post->post_type != 'page'){
        return;
    }

    // If you get here then it's your post type so do your thing....
    // unhook this function so it doesn't loop infinitely
    remove_action('post_updated', 'save_post_callback');

    $title = $post_after->post_title;
    // Check if "NTS00" is already there,
    // Deliberately checking if position found is > 0
    if( strpos($title, "NTS00") == FALSE ){
        // Set the title
        $args = array(
            'ID'           => $post_ID,
            'post_title'   => $title.'_NTS000'.$post_ID
        );
        // update the post, which calls save_post again
        wp_update_post( $args );
        // Bonus! 
        update_post_meta( $post_ID, 'my_key', 'NTS000'.$post_ID );
        // re-hook this function
        add_action('post_updated', 'save_post_callback');

    }
}

This way you will ensure that the NTS00 gets appended only once. Bonus feature will ensure that you also store that value for a "custom key" as post meta.

I hope I was able to help.

You could use something like Gravity Forms Unique ID to generate a unique sequential ID when the form is submitted.

You could then use the merge tag for this Unique ID field in your Post Title field's content template to include it as part of the generated post title.

发布评论

评论列表(0)

  1. 暂无评论