Search code examples
phpwordpressadvanced-custom-fields

Creating function to replace post title not working


I am trying to create a function inside functions.php to replace the custom post title with information from several custom fields. Before publishing the post, I want to evaluate if it is already created or if this is a new post; if it is a new post, I want to get information from several fields, count the total number of posts in this custom post type and create the title; if this is just editing an already existing post, I want to use the content inside the $_POST['post_title'] field.

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_title' => $new_title,
            '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);

It does work for already created posts but it is not running the part of the function that creates the custom post title. Any suggestions? Thanks in advance!

I replaced my function with the one KSNO suggested:

<?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_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
    // http://codex.wordpress.org/Function_Reference/wp_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 );
  }
}
add_action( 'publish_post', 'title_replace_function', 10, 2 );

?>

When it evaluates if the post is not "NEW" it works correctly. But it is not getting the custom fields values to create a new title for the new posts. the title field is empty. I even tried adding the value of one custom field to the '$new_title' variable and nothing


Solution

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

    Can't ever happen. If false is being returned, it means post doesnt exist. You can't really update a post that doesn't exist. Check source code of the function.

    There are few ways, I think, to get your task done but I'll just suggest one of them.

    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', 'title_replace_function', 10, 2 );
    

    EDIT

    Alright, wasn't that easy as I thought. Here is tested, secured from infinite loops (Be aware of loops Example 1 and Example 2) piece of code, that suits your needs:

    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_title' => $new_title,
                    'post_name'  => $post_id
                );
                wp_update_post( $my_post );
            } else {
                $new_title = 'new_title';
                $my_post = array(
                    'ID'         => $post_id,
                    'post_title' => $new_title,
                    'post_name'  => $post_id
                );
                wp_update_post( $my_post );
            }
    
            add_action('save_post', 'my_function');
        }
    }
    add_action( 'save_post', 'title_replace_function', 10, 3 );