2

I have custom fields in my custom post type. They should serve as a title and post slug for URL. Basically, whenever I change the content of the custom fields, the title field should react accordingly. E.g. if the field NAME contains 'John' and the SURNAME 'Smith', the title should transform to 'John Smith'.

The users of the site will be unexperienced so they won't understand the need of putting NAME and SURNAME and a separate title for that - that's why I need it.

I know I can display custom field content instead of a title in my template, but that's not the same. I also need the permalink to be amended accordingly.

So, to recap My input: custom field NAME = John custom field SURNAME = Smith What should happen: post title = John Smith permalink = john-smith (I presume it will process the title extracted from the custom fields as it would normally do)

I hope it's clear now. Thanks indeed for any suggestions.

3
  • I think the title of your quetion and the description are not about the same issue. The permalink structure and the title of the pages are different things. So, what do you want to change? The title of the pages as you describe in the description of the question or the URL structure/permalink as your describe in the title of the question?
    – cybmeta
    May 11, 2014 at 9:19
  • Please, edit your question to match your requeriments. The title and the description of the question and your last comment is all differents things, so we can't know what is your problem and the helop you need.
    – cybmeta
    May 11, 2014 at 12:08
  • Is it understandable now? May 11, 2014 at 13:30

3 Answers 3

1

I think a good way to do what you want is hook the wp_insert_post_data filter. Here you can change the title and slug of the post before it is saved to database:

add_filter( 'wp_insert_post_data', 'wpse_update_post_title_and_slug' );
function wpse_update_post_title_and_slug( $data ) {

    //You can make some checks here before modify the post data.
    //For example, limit the changes to standard post type
    if( 'post' == $data['post_type'] ) {

        //Add post meta to post title
        $data['post_title'] = $_POST['the-name-field'] . ' ' . $_POST['the-surname-field'];

        //Update the slug of the post for the URL
        $data['post_name'] = sanitize_title( $data['post_title'] );

    }

    return $data;

}
4
  • WOW! Didn't know about this powerful function. Thanks for the meaningful example, I'll explore it :) May 12, 2014 at 9:27
  • Let me know if this answers your question.
    – cybmeta
    May 12, 2014 at 9:42
  • Nevermind all the posts. It's working properly now. It was my mistake. The case is, that it works only when there's something in the title field (doesn't matter what). I understand why it happens, but this title field is to be scrapped from the editor... any ideas how to force it? I tried using if title is empty, then... but it didn't help. May 12, 2014 at 17:55
  • I forget to remove $postarr. In the code I posted only one argument for wp_insert_post_data filter is supported, so the second should be removed or add support to second argument with add_filter( 'wp_insert_post_data', 'wpse_update_post_title_and_slug', '99', 2 );. I've tested the code and it works correctly. If you want to debug this filter you can use the same function described here.
    – cybmeta
    May 13, 2014 at 7:44
0

There is an alternative, don't set the title at all. Instead, filter it.

Here is the_title filter as demonstrated by the codex:

function suppress_if_blurb($title, $id) {
    if (in_category('blurb', $id)) {
        return '';
    }
    return $title;
}
add_filter('the_title', 'suppress_if_blurb', 10, 2);

If instead we did something like this:

function wpse144041_modify_title($title, $id) {
    if ( get_post_type( $id ) == 'yourposttype' ) {
        $first = get_post_meta( $id, 'first', true );
        $last = get_post_meta( $id, 'last', true );
        $title = $first.' '.$last;
    }
    return $title;
}
add_filter('the_title', 'wpse144041_modify_title', 10, 2);

Then the title field is now irrelevant, and will show the first and last names as the title on the front end for your post type. Adjust the relevant field and post type names to match

1
  • He needs also the URL slug with the name-surname meta fields. Although you can filter the permalink and combine with custom rewrite rules or query vars to get the correct post, I think is better and much less work having the correct post_name (the post URL slug) in the database, thats why I thought in wp_insert_post_data filter. This also make manage canonical URLs and redirections with much less effort if needed in the future.
    – cybmeta
    May 12, 2014 at 9:38
-2

I'm afraid the code above didn't work at all. I modified it like that:

add_filter( 'wp_insert_post_data', 'wpse_update_post_title_and_slug' );
function wpse_update_post_title_and_slug( $data , $postarr ) {

    //You can make some checks here before modify the post data.
    //For example, limit the changes to standard post type
    if( 'people' == $data['post_type'] ) {

        //Add post meta to post title
        $data['post_title'] = $_POST['first_name'] . ' ' . $_POST['last_name'];

        //Update the slug of the post for the URL
        $data['post_name'] = sanitize_title( $data['post_title'] );

    }

    return $data;

}

Despite the fact that the particular custom fields are present and have content, neither the title nor the slug changes.

I assumed that both would be amended "by force" upon saving the post? Or maybe it doesn't work that way?

I also tried to re-code it like this:

function wpse_update_post_title_and_slug( $data , $postarr ) {

if( $data['post_type'] == 'people' ) {
// get post data
$id = $postarr['ID'];
$first_name = get_post_meta( $id, 'first_name', true );
$last_name = get_post_meta( $id, 'last_name', true );
// insert the data into the title and the slug
$data['post_title'] = $first_name . ' ' . $last_name;
$data['post_name'] = sanitize_title( $data['post_title'] );
    }

    return $data;

}
add_filter( 'wp_insert_post_data', 'wpse_update_post_title_and_slug' );

But it's all the same. No reaction, no error, no nothing. As it was totally absent from functions.php. It works ordinarily.

1
  • This is not an answer to your question, it is an update to the question, so you should edit the question to add new information and code or it will attrack downvotes.
    – cybmeta
    May 13, 2014 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.