Auto-generated title and permalinks for CustomPress post type

Hello,
I'm trying to create a custom post type for meeting minutes that are recorded at monthly meetings. I am looking to have each post type's title and permalink automatically generated according to the month in which the post was published. For example, if meeting minutes (the post type) are posted on March 5th, 2016 - the Title of the post will be "March 2016" and the permalink will be /[category name]/[year]/[month number] to produce: "/association/2016/03"

I've been digging pretty deep, but haven't found any examples where this has been done - is this possible?

Thanks

  • Ash

    Hello Christopher

    I have just tested and got the correct title and permalink. It seems the title is generated from your theme or any other plugin. Would you please check the page using default wp theme?

    If you still see the same, please run a plugin conflict test. Please deactivate all plugins except this one and check if you still face the problem. If not, then enable one by one and check which one is causing the problem. You can see the flowchart here: http://premium.wpmudev.org/manuals/using-wpmu-dev/getting-support/

    Please let me know what you get.

    Cheers
    Ash

  • Ash

    Hi there

    I am sorry I thought the opposite :slight_frown:

    Okay, to do so you have to write some custom code. Let me explain:
    1. I assume your custom post type is "cpt" and custom taxonomy (category or whatever) is cpt_tax
    2. In Dashboard > CustomPress > Content Types > Edit CPT post type and in "Rewrite" module, Custom Slug section, put the following:
    /%cpt_tax%/%year%/%month%
    3. Now use the following code:

    add_action( 'save_post', 'change_title' );
    function change_title( $post_id ) {
        $post = get_post( $post_id );
    
        $month = date( 'F', strtotime( $post->post_date ) );
        $year = date( 'Y', strtotime( $post->post_date ) );
    
        remove_action( 'save_post', 'change_title' );
    
        wp_update_post(
            array(
                'ID' => $post_id,
                'post_title' => $month . ' ' . $year
            )
        );
    
        add_action( 'save_post', 'change_title' );
    }
    
    add_filter( 'post_type_link', 'cpt_filter_post_type_link', 10, 2 );
    function cpt_filter_post_type_link( $link, $post ) {
        if ( $post->post_type == 'cpt' ) {
            if ( $cats = get_the_terms( $post->ID, 'cpt_tax' ) ) {
                $month = date( 'F', strtotime( $post->post_date ) );
                $year = date( 'Y', strtotime( $post->post_date ) );
                $link = str_replace(
                            array( '%cpt_tax%', '%year%', '%month%' ),
                            array( strtolower( current( $cats )->slug ), $year, strtolower( $month ) ),
                            $link
                        );
            }
        }
    
        return $link;
    }
    
    add_filter( 'rewrite_rules_array', 'add_rewrite_rules' );
    function add_rewrite_rules( $rules )
    {
        $new = array();
        $new['(.+)/(.+)/(.+)/?$'] = 'index.php?cpt=$matches[3]';
    
        return array_merge( $new, $rules );
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    You also need to change cpt and cpt_tax with correct custom post type and taxonomy name from your site.

    After using the above code, do not forget to flush your permalink :slight_smile:

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Ash

    Hello Christopher

    Please try the following code:

    add_action( 'save_post', 'change_title' );
    function change_title( $post_id ) {
        $post = get_post( $post_id );
    
        if( $post->post_type != 'XXXXXX' ) return;
    
        $month = date( 'F', strtotime( $post->post_date ) );
        $year = date( 'Y', strtotime( $post->post_date ) );
    
        remove_action( 'save_post', 'change_title' );
    
        wp_update_post(
            array(
                'ID' => $post_id,
                'post_title' => $month . ' ' . $year
            )
        );
    
        add_action( 'save_post', 'change_title' );
    }
    
    add_filter( 'post_type_link', 'cpt_filter_post_type_link', 10, 2 );
    function cpt_filter_post_type_link( $link, $post ) {
        if ( $post->post_type == 'cpt' ) {
            if ( $cats = get_the_terms( $post->ID, 'cpt_tax' ) ) {
                $month = date( 'F', strtotime( $post->post_date ) );
                $year = date( 'Y', strtotime( $post->post_date ) );
                $link = str_replace(
                            array( '%cpt_tax%', '%year%', '%month%' ),
                            array( strtolower( current( $cats )->slug ), $year, strtolower( $month ) ),
                            $link
                        );
            }
        }
    
        return $link;
    }
    
    add_filter( 'rewrite_rules_array', 'add_rewrite_rules' );
    function add_rewrite_rules( $rules )
    {
        $new = array();
        $new['(.+)/(.+)/(.+)/?$'] = 'index.php?cpt=$matches[3]';
    
        return array_merge( $new, $rules );
    }

    Replace XXXXXX with the custom post type you are using in custompress :slight_smile:

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.