我正在尝试在内部创建一个函数functions.php
,用来自多个自定义字段的信息替换自定义帖子标题.在发布帖子之前,我想评估它是否已经创建或者这是一个新帖子; 如果是新帖子,我想从多个字段中获取信息,计算此自定义帖子类型中的帖子总数并创建标题; 如果这只是编辑已经存在的帖子,我想使用该$_POST['post_title']
字段内的内容.
function custom_field_value_as_title( $value, $post_id, $field ) {
global $_POST;
// vars
$title = $_POST['post_title'];
$post = get_page_by_title( $title, 'OBJECT', 'mascotas' );
if ( FALSE === get_post_status( $post_id ) ) {
$new_title_ciudad = get_field('ciudad', $post_id);
$new_title_sexo = get_field('sexo', $post_id);
$new_title_especie = get_field('especie' , $post_id);
$registered_year = date("y");
$count_mascotas = wp_count_posts('mascotas');
$next_count = $count_mascotas->publish + 1;
$new_count = sprintf("%04d", $next_count);
$new_title = "$new_title_ciudad"."$new_title_sexo"."$new_title_especie"."$registered_year"."-"."$new_count";
// update post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title, …
Run Code Online (Sandbox Code Playgroud)