automatically add custom fields to post title

admin2025-01-07  7

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.

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.

Share Improve this question edited Mar 7, 2018 at 17:01 juicebyah asked Mar 7, 2018 at 14:51 juicebyahjuicebyah 1114 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255909a319.html

最新回复(0)