• Resolved mrpritchett

    (@mrpritchett)


    I am working on a ticket submission system for my organization (and possibly a WP-theme) and would like to use the POST ID as the POST TITLE. Posts are posted from the front end based on a similar coding to https://voodoopress.com/2011/03/review-of-posting-from-front-end-form/ . I tried to set it using a variable, but I gets set to no title. See below:

    $new_post = array(
    	'ticket_number' => $ticket_number,
        $post->ID  =>   $title,
        'post_content'  =>   $description,
    	'post_category' => array($_POST['cat']),
    	'location' => array($_POST['location']),
    	'emergency' => array($_POST['emergency']),
    	'problem_type' => array($_POST['problem_type']),
        'tags_input'    =>   array($tags),
        'post_status'   =>   'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' =>   'post',  //'post',page' or use a custom post type if you want to
        $emergency = $_POST['emergency'],
    	$location = $_POST['location'],
    	$problem_type = $_POST['problem_type'],
        );

Viewing 4 replies - 1 through 4 (of 4 total)
  • Not sure exactly what you are doing but this seems incorrect:
    $post->ID => $title,

    you want to assign the post ID as the title. that would be more like

    'ticket_number' => $ticket_number,
    'post_title' => $post->ID,

    Thread Starter mrpritchett

    (@mrpritchett)

    Thank you for the reply stvwlf! The problem with this is that it returns the current page ID. I am posting from the front end through a page template. I want to make the post ID the title of the post. The problem that I am having is that the post ID is not created until the post is published, which means the title is set (I believe this is how it works). Is there a way to set the title after the post ID and then set the ID as the post title?

    You will have to update the title after the post is inserted, because the ID is not known until after the post is inserted.

    According to the Codex wp_insert_post() returns the id of the new post.

    Here is info about updating an existing post:
    https://codex.www.ads-software.com/Function_Reference/wp_update_post

    You need to create an array with the fields you want to update and then call wp_update_post using the array as parameter

    start with this line from Rev Voodoo’s code (its in a part of your code you didn’t post):
    $pid = wp_insert_post($new_post);

    add these lines after that line

    $update_post = array();   // create empty array
    $update_post['ID'] = $pid;   // set post ID to be updated
    $update_post['post_title'] = $pid;  // set new value for title in that post
    wp_update_post( $update_post );  // update the post

    See if that works – code is not tested.

    Thread Starter mrpritchett

    (@mrpritchett)

    stvwlf: This worked! Thanks for the help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Publish Post ID as the Post Title’ is closed to new replies.