• I have successfully wrote a little function that renames my post title and url (permalinks) based on the category and custom fields.

    It works well however i imagine i will encounter problems soon as a post url could be set the same if the custom fields + category chosen are the same as an existing post. Thus I would need to include the post id in the url to ensure this doesnt happen (not in the title as this would look silly..), however I cant get it to work.

    I would also like to include the £ symbol in the title [Not the url…(extra question: can you actually use a £ symbol in a url?)]

    the code (this works.. but doesnt have the above..)

    //Auto add and update Title field:
      function my_post_title_updater( $post_id ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
    
        $category        = get_the_category();
        $model           = get_field('model');
        $price           = get_field('price');
        if ( get_post_type() == 'post' ) {
    
          $my_post['post_title'] = $category[0]->cat_name . ' ' . get_field('model') . ' ' . get_field('price');
          $my_post['post_name'] = $category[0]->cat_name . '-' . get_field('model') . '-' . get_field('price');
    }
        // Update the post into the database
        wp_update_post( $my_post );
    
      }

    Extra question 2: If i choose multiple categories when i add my post would this cause my code to malfunction or should it be okay? (This is simply for reference as in my current usage it would never happen).

    Any ideas or pointers would be great!
    thanks,
    uf

  • The topic ‘title and url rewrite, (working) need to include post id and £ symbol’ is closed to new replies.