I have two custom post type. Then I need to use title_save_pre to post title prior to saving it in the database. I need to use this filter just for one custom post type.
This is my function:
<?php
function muda_titulo() {
global $post;
$type = get_post_type($post->ID);
if ($type== 'event') {
$title = $post->post_excerpt;
$day= get_the_time('l, d F, Y');
return $title.' - '.$day;
} else if ($type == 'post') {
// do nothing
}
}
add_filter ('title_save_pre','muda_titulo');
?>
On custom post type ‘event’ it works fine, but on custom post type ‘post’ the title changed to a white space.
Thank u
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try the code below. Filter’s take a value and returns it afterwards.
<?php
function muda_titulo($title) {
global $post;
$type = get_post_type($post->ID);
if ($type== 'event') {
$title = $post->post_excerpt;
$day= get_the_time('l, d F, Y');
return $title.' - '.$day;
} else if ($type == 'post') {
return $title;
}
}
add_filter ('title_save_pre','muda_titulo');
?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0