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.
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