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.

1

2 Answers 2

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.