9

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

Mansuro
  • 4,558
  • 4
  • 36
  • 76

2 Answers2

3
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 );
ksno
  • 487
  • 1
  • 4
  • 21
  • Thanks ksno! what I ment to write is that if FALSE is returned (meaning that the post doesn't exists) to replace post title with the content from the custom fields. if it returns TRUE, then just update with the content of $_POST['post_title'] field – Gustavo Cohen Jul 12 '16 at 12:21
  • I am using your function but it is not generating the new title: – Gustavo Cohen Jul 12 '16 at 12:35
  • I am using your function but it is not generating the new title. When I create a new post, after I publish the post the title field is empty – Gustavo Cohen Jul 12 '16 at 12:46
  • @GustavoCohen check edit. I think your problem should be solved. – ksno Jul 13 '16 at 07:26
  • Hi ksno! I am going to try your function today. Thanks so much for taking the time to write the code! – Gustavo Cohen Jul 15 '16 at 21:05
  • Hi ksno! works perfect! thanks alot! The only thing I see needs fixing is something related to the counter of posts. it counts the total number of posts by the user currently logged in. I have 2 posts created by the admin and 74 posts created by another user. the next post created shows -0075 instead of -0077 so I am assuming it is not considering the 2 posts created by me. Any thoughts? – Gustavo Cohen Jul 25 '16 at 17:23
  • I think its another question you should post, as this one **How to replace title on new post/post edit?** was answered. – ksno Jul 26 '16 at 07:50
0

I was looking for the wrong thing - i hope this helps someone using ACF / Advanced Custom Fields. Turns out ACF fields aren't saved yet, when save_post runs - ACF comes to help us here with acf/save_post. With this hook the ACF fields are available and can be used to set a new title, in this case a date.

This updates on first save and on revisions too - so the date/title always match, even if changed.

add_action( 'acf/save_post', [ $this, 'set_post_title' ], 10, 3 );

function set_post_title( $post_id ) {
        $post = get_post( $post_id );

        /* @var $parent_post_id integer */
        $parent_post_id = $post->post_parent;

        if ( ! $parent_post_id ) {
            $parent_post_id = $post_id;
        }

        /* @var $post_type string */
        $post_type = get_post_type( $parent_post_id );

        if ( 'post' !== $post_type ) {
            return;
        }

        $fields = get_fields( $parent_post_id );

        $date      = $fields['date'] ?? 'DATE MISSING';
        $new_title = $date . ' (' . $post_id . ')';

        $post_update = array(
            'ID'         => $parent_post_id,
            'post_title' => $new_title
        );

        wp_update_post( $post_update );
    }
Frizzant
  • 684
  • 1
  • 7
  • 28