0

im going to add custom fields to post title ,

my post title is "Marroon Five" and my custom fields is "release-year" and with value "2017" .

so how to make the output : Marroon Five (2017)

this is what i found after doing some searching

add_action('save_post', 'update_term_title');
function update_term_title($post_id)
{
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(!current_user_can('edit_post', $post_id))
        return;

    //Replace 'release-year' with whatever your custom taxonomy slug is
    $terms = wp_get_post_terms($post_id, 'release-year');

    if(empty($terms))
        return;

    $title = false;
    foreach($terms as $term)
    {
        if($term->parent)
        {
            $parent = get_term($term->parent, 'release-year');
            $title = $term->name.' '.$parent->name;
            break;
        }
    }
    /*Default to first selected term name if no children were found*/
    $title = $title ? $title : $terms[0]->name;

    /*We must disable this hook and reenable from within
    if we don't want to get caught in a loop*/
    remove_action('save_post', 'update_term_title');
    $update = array(
        'ID'=>$post_id,
        'post_name'=>sanitize_title_with_dashes($title),
        'post_title'=>$title
    );
    wp_update_post($update);
    add_action('save_post', 'update_term_title');
}

the only problem is this code replace all the post title with custom field value which is "2017"

hope someone can fix this is return output as i wanted to

thank you.

2
  • This should help: wordpress.stackexchange.com/a/198268/45202. The problem is caused by setting the ~$title~ to false instead of concatenating the term to the end of the existing title. Fetch the title, add to it and return it.
    – jdm2112
    Commented Mar 7, 2018 at 16:16
  • sorry im not really understand , can you explain it a little bit sir
    – juicebyah
    Commented Mar 7, 2018 at 17:03

1 Answer 1

0

You could write a function to hook into the display of the title, and then when the title is displayed, it will get the value of the custom field and append it to the title. That way you aren't editing/changing the title every time you save the post.

You could do something like:

function append_year( $title ) {
    global $post;
    $terms = wp_get_post_terms( $post->ID, 'release-year', array("fields" => "names") );

    if ( count( $terms ) ) {
        return $title . '(' . $terms[0] . ')';
    } else {
        return $title;
    }
}

add_filter( 'the_title', array($this, 'append_title' ) );

wp_get_post_terms returns an array, and you can pass it an array of args to configure what you want see the wp_get_post_terms documentation.

The code above was adapted from this other SO post about appending title text

3
  • Fatal error: Uncaught Error: Using $this when not in object context got an error when put on functions.php mate
    – juicebyah
    Commented Mar 8, 2018 at 16:24
  • Try it just as add_filter( 'the_title', 'append_title' ); Commented Mar 9, 2018 at 21:56
  • sorry for the late reply , after i changed the code , no error on the output but the problem is still doesnt auto add spesific custom fields to post title , i think i have to do that manually
    – juicebyah
    Commented Mar 15, 2018 at 12:19

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.