WordPress.org

Ready to get started?Download WordPress

Forums

Updating post_name to include post ID almost got it but need help (2 posts)

  1. Pete
    Member
    Posted 1 year ago #

    I'm using a form to create posts. Before the redirect i'm updating the post_name and including the post ID at the end. The code below is almost working. It's creating a post name like post_name-48 so I got the post ID there but why isn't post_name displaying as the post name and only as a string. I pieced together the function below the best I could and help is appreciated. Thanks

    // Update post slug
    $my_post['ID'] = $pid;
    $my_post_name = 'post_name' . "-" . $pid;
    $my_post['post_name'] = $my_post_name;
    
    // Update the post into the database
      wp_update_post( $my_post );
  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think you need to use the .= operator to add your term to the post name:

    $my_post['post_name'] .= $my_post_name;

    Looks like you may also need a space in front of 'post_name': ' post_name'

Topic Closed

This topic has been closed to new replies.

About this Topic