0

I am currently working on a project, where I need to do this -

  • Create a custom post type called authors
  • Create first name and last name field for authors
  • Single author permalink structure must be first_name_field-last_name_field. e.g - author Arafat Jamil must have permalink arafat-jamil

How can I achieve this? Third party plugins must not be used, but you can name plugins, I can take the functionality out of it.

8
  • My targeted approach is to split the 'Title' field into two parts, that will create the first name and last name fields. If you have any idea, you can respond of course. Oct 14, 2022 at 11:14
  • So you mean, a full permalink URL would look like https://example.com/first-last/ (no base), or is it like https://example.com/authors/first-last/ (base is authors)? What are these authors - authors of what? And as for splitting the post title, are you sure the author's name will always be first last and not last first or first middle last?
    – Sally CJ
    Oct 14, 2022 at 15:39
  • Authors, is a custom post type that I will build myself, it is not related to WordPress roles directly. You can think of this as a profile storage system, for example, you can consider a list of Employees. And I want to split the title with 2 different labels, First name and Last name. Oct 14, 2022 at 15:54
  • 1
    Well by default, if the post title is John Doe, then WordPress will use john-doe as the post slug that's used in the permalink. As for creating the authors post type, you can use register_post_type(), and there are generators like this that you can quickly try. As for the first/last names, you can add them either as custom fields or terms in a custom taxonomy.
    – Sally CJ
    Oct 14, 2022 at 16:53
  • I have already created a post type. I have also added meta boxes for other fields which were necessary. But If I can split the title, I won't need to work on the slug separately. If I take first and last name differently, I will need to change the slug again. Is there any way to split the title, may be in two parts of a form, and a third hidden field to connect the value of those two, before it is sent to database? Oct 14, 2022 at 19:33

1 Answer 1

0

I have been working on this solution, I have come up with a different idea to implement this -

I have added two different meta boxes, one with First Name, the other with Last Name

Then I have hooked a function with save_post_{$post->post_type} hook like this -

add_action( "save_post_{$post->post_type}", "update_post_data" );

function update_post_data( $post_id ) {
    // Unhook to stop infinite loop.
    remove_action(  "save_post_{$post->post_type}", "update_post_data" );

    $fname = sanitize_title( get_post_meta( $post_id, '_first_name', true ) );
    $lname = sanitize_title( get_post_meta( $post_id, '_last_name', true ) );

    $post_title = ucwords( $fname . ' ' . $lname );
    $post_name  = $fname . '-' . $lname;

    $args = [
        'post_title' => $post_title,
        'post_name'  => $post_name,
    ];

    // Update post data.
    wp_update_post( $args );

    // Re-hook the function, to make it work in future.
    add_action( "save_post_{$post->post_type}", "update_post_data" );
}

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.