Forums

[resolved] Publish Post ID as the Post Title (5 posts)

  1. mrpritchett
    Member
    Posted 11 months ago #

    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 http://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'],
        );
  2. stvwlf
    Member
    Posted 11 months ago #

    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,
  3. mrpritchett
    Member
    Posted 11 months ago #

    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?

  4. stvwlf
    Member
    Posted 11 months ago #

    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:
    http://codex.wordpress.org/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.

  5. mrpritchett
    Member
    Posted 11 months ago #

    stvwlf: This worked! Thanks for the help!

Reply

You must log in to post.

About this Topic