• Pete

    (@perthmetro)


    I have this nifty function that automatically names a normal post type according to date, Id and author name etc. How would I edit this so that only the custom post types XYZ and ABC are renamed?

    // auto post title by date,author name and post id
    add_action( 'wpuf_add_post_after_insert', 'wpuf_alter_title' );
    add_action( 'wpuf_edit_post_after_update', 'wpuf_alter_title' );
    
    function wpuf_alter_title ( $post_id){
    	$post_object=get_post($post_id);
        $post_date      = $post_object->post_date;
        $format_date    = DateTime::createFromFormat( 'Y-m-d H:i:s', $post_date );
        $date_formatted = $format_date->format( 'Y-m-d' ); // Set correct to display here
    
        $post_author    = $post_object->post_author;
        $author_name    = get_the_author_meta( 'display_name', $post_author ); // Adjust as needed
    
        $my_post = array(
            'ID' => $post_id,
            'post_title' =>  '#' .  $post_id . ' ' . $author_name  . ' ' . $date_formatted // Change as needed
    );
    	// unhook this function so it doesn't loop infinitely
    	remove_action('save_post', 'wpuf_alter_title');
    
    	wp_update_post( $my_post );
    
    	// re-hook this function
    	add_action('save_post', 'wpuf_alter_title');
    
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • You could use a pretty simple if() statement in there to check if it’s the right post type.

    $allowed_types = array (
        'typex',
        'typey',
        'typez'
    );
    
    if ( in_array ($post_object->post_type, $allowed_types ) [
        // Update title, etc in here.
    }
    Thread Starter Pete

    (@perthmetro)

    Like this?

    // auto post title by date,author name and post id
    add_action( 'wpuf_add_post_after_insert', 'wpuf_alter_title' );
    add_action( 'wpuf_edit_post_after_update', 'wpuf_alter_title' );
    
    function wpuf_alter_title ( $post_id){
    $allowed_types = array (
        'typex',
        'typey',
        'typez'
    );
    	$post_object=get_post($post_id);
        $post_date      = $post_object->post_date;
        $format_date    = DateTime::createFromFormat( 'Y-m-d H:i:s', $post_date );
        $date_formatted = $format_date->format( 'Y-m-d' ); // Set correct to display here
    
        $post_author    = $post_object->post_author;
        $author_name    = get_the_author_meta( 'display_name', $post_author ); // Adjust as needed
    
        $my_post = array(
            'ID' => $post_id,
            'post_title' =>  '#' .  $post_id . ' ' . $author_name  . ' ' . $date_formatted // Change as needed
    );
    	// unhook this function so it doesn't loop infinitely
    	remove_action('save_post', 'wpuf_alter_title');
    if ( in_array ($post_object->post_type, $allowed_types ) {
        // Update title, etc in here.
    
    	wp_update_post( $my_post );
    }
    	// re-hook this function
    	add_action('save_post', 'wpuf_alter_title');
    
    }

    That looks OK from that, but you’d need to test it.

    The only suggestion that I’d make is to wrap pretty much all of the code in the if() statement so that you don’t do any more processing then you need to. That’s something that’s not at all essential, just the developer part of me thinking too much about optimisation. 🙂

    Thread Starter Pete

    (@perthmetro)

    OK thanks 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How do I restrict this function to a specific CPT?’ is closed to new replies.