0

I have a problem, my custom title keeps getting added at every time I save the post..

First I created a Custom Post Type called 'imac' plus a front-end form for posting in this Post Type, only before saving I would like to change the ['post_title'] based on the Custom Fields people fill out on the front-end form aswel.

Every post people create front-end will get a 'pending' post_status but as soon as I press Publish or edit the post later and press update the title gets added too. for example: example.com/imac-lala-blabla/ becomes after a second save: example.com/imac-lala-blabla-lala-blabla/

Can somebody help me please.

function modify_imac_title( $post_id ) {

// ['post_title'] create $vars for custom title setup

    $ghz = get_post_meta( $post_id, 'imac_processor', true );
    $strip_ghz = preg_replace( '/z.*$/', 'z', $ghz );
    $ram = get_post_meta( $post_id, 'imac_ram', true );
    $hd = get_post_meta( $post_id, 'imac_harddrive', true );
    $model = get_post_meta( $post_id, 'imac_model', true );

    global $wpdb;

    $wpdb->update( $wpdb->posts,
                    array(
                        'post_title' => get_the_title( $post_id ).' '.$strip_ghz.' '.$ram.'GB '.$hd.' '.$model
                    ),
                    array( 'ID' => $post_id )
                ); 

}
add_action( 'save_post_imac', 'modify_imac_title' );

2 Answers 2

0

First of: Your custom title suffix getting duplicated might not be what you want to happen, but is expected behavior as per your code sample.

The save_post action hook runs everytime a post (or page) is created or updated. Hence your callback runs everytime.

Let me offer two solutions:

1 Use the wp_insert_post action instead

The wp_insert_post action is called with the same parameters as the save_post action (the post ID for the post being created), but is only called for new posts and only after save_post has run.

Note that, this hook does not have a posttype-specific equivalent, and hence you'd have to check for the type inside the callback function.

function modify_imac_title( $post_id ) {

    if ( 'imac' === $_POST['post_type'] ) {

        // do your thing

    }

}
add_action( 'wp_insert_post', 'modify_imac_title' );

2. Add a boolean post_meta

Add a boolean post_meta that signifies, whether the title modification has run already.

function modify_imac_title( $post_id ) {

    $title_modified = get_post_meta( $post_id, 'title_modified', true );

    if ( ! $title_modified ) {

       update_post_meta( $post_id, 'title_modified', true );

       // do your thing

    }

}
add_action( 'save_post_imac', 'modify_imac_title' );
1
  • Thank you for helping me. The first example was really clear and works perfect! Commented Sep 1, 2014 at 15:12
-1

I can't comment yet, so I ll try to guess that the one that is getting duplicated is the post and not just the post title.

Try this:

function modify_imac_title( $post_id ) {

// ['post_title'] create $vars for custom title setup

$ghz = get_post_meta( $post_id, 'imac_processor', true );
$strip_ghz = preg_replace( '/z.*$/', 'z', $ghz );
$ram = get_post_meta( $post_id, 'imac_ram', true );
$hd = get_post_meta( $post_id, 'imac_harddrive', true );
$model = get_post_meta( $post_id, 'imac_model', true );

global $wpdb;
        remove_action( 'save_post_imac', 'modify_imac_title');
$wpdb->update( $wpdb->posts,
                array(
                    'post_title' => get_the_title( $post_id ).' '.$strip_ghz.' '.$ram.'GB '.$hd.' '.$model
                ),
                array( 'ID' => $post_id )
            ); 
        add_action( 'save_post_imac', 'modify_imac_title');
}
add_action( 'save_post_imac', 'modify_imac_title' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.