php – 创建函数来替换帖子标题不起作用

前端之家收集整理的这篇文章主要介绍了php – 创建函数来替换帖子标题不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在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,'post_name'  => $post_id
            );

    // Update the post into the database
        wp_update_post( $my_post );

    } else {
    // vars             
        $new_title = $_POST['post_title'];

    // update post
        $my_post = array(
            'ID'         => $post_id,'post_name'  => $post_id
            );

    // Update the post into the database
        wp_update_post( $my_post );
    }

}

add_filter('acf/update_value','custom_field_value_as_title',10,3);

它适用于已创建的帖子,但它没有运行创建自定义帖子标题功能部分.有什么建议?提前致谢!

我用KSNO建议替换了我的函数

<?PHP

function title_replace_function( $post_id,$post ) {
   if ( $post->post_date == $post->post_modified ) {

    global $_POST;

    $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_name'  => $post_id
    );

    // Update the post into the database
    wp_update_post( $my_post );

} else {

    // vars             
    $new_title = $_POST['post_title'];

    // update post
    // http://codex.wordpress.org/Function_Reference/wp_update_post
    $my_post = array(
    'ID'         => $post_id,'post_name'  => $post_id
    );

    // Update the post into the database
    wp_update_post( $my_post );
  }
}
add_action( 'publish_post','title_replace_function',2 );

?>

当它评估帖子是否不是“新”时,它可以正常工作.但它没有获取自定义字段值来为新帖子创建新标题.标题字段为空.我甚至尝试将一个自定义字段的值添加到’$new_title’变量中,什么都没有

if ( get_post_status( $post_id ) === FALSE ) {
    wp_update_post( $my_post );
}

不可能发生.如果返回false,则表示post不存在.您无法真正更新不存在的帖子. Check source code of the function.

我认为,很少有方法可以完成任务,但我会建议其中一个.

function title_replace_function( $post_id,$post ) {
   if ( $post->post_date == $post->post_modified ) {
      // code for new post
   } else {
      // code for edited post
   }
}
add_action( 'publish_post',2 );

编辑

好吧,并不像我想的那么容易.这里经过测试,从无限循环(注意循环Example 1Example 2)中获得一段代码,以满足您的需求:

function title_replace_function( $post_id,$post ) {
    if ( ! wp_is_post_revision( $post_id ) ) {

        remove_action('save_post','title_replace_function');

        if ( $post->post_date == $post->post_modified ) {
            global $_POST;
            $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";
            $my_post = array(
                'ID'         => $post_id,'post_name'  => $post_id
            );
            wp_update_post( $my_post );
        } else {
            $new_title = 'new_title';
            $my_post = array(
                'ID'         => $post_id,'post_name'  => $post_id
            );
            wp_update_post( $my_post );
        }

        add_action('save_post','my_function');
    }
}
add_action( 'save_post',3 );
原文链接:https://www.f2er.com/php/136518.html

猜你在找的PHP相关文章