0

I created a custom post type and specified a rewrite to change the permalink to /old-donuts/. See code below:

function donut_newsletters_post_type() {

  $labels = array(
    'name' => '🍩 Newsletters',
    'singular_name' => '🍩 Newsletter',
    'add_new' => 'Add 🍩',
    'add_new_item' => 'Add New 🍩',
    'edit_item' => 'Edit 🍩',
    'new_item' => 'New 🍩',
    'view_item' => 'View 🍩',
    'search_item' => 'Search Daily 🍩s',
    'not_found' => 'No 🍩s found',
    'not_found_in_trash' => 'NO 🍩s found in the trash..that would be a sin'
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'has_archive' => false,
    'publicly_queryable' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'old-donuts'
    ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array(
      'title',
      'editor',
      'excerpt',
      'thumbnail'
    ),
    'taxonomies' => array('post_tags'),
    'menu_position' => 0,
    'exclude_from_search' => true,
    'menu_icon'   => 'dashicons-email'
  );

  register_post_type('donut_newsletters', $args);
}

add_action('init', 'donut_newsletters_post_type');

Now let's say I go create a new post and title it "test", the permalink defaults to https://thedonut.co/old-donuts/test

I don't want this. What I do want is for the permalink to instead default to a custom date structure like this:

https://thedonut.co/old-donuts/2019-08-24/

I also want the keep the permalink modifiable by the editor so that in the case that he/she had to create a post the day before they want it to go live, they can change the day value to the next day.

Please don't send me to a plugin. I've tried custom post type permalinks plugin and it does what i want, but it doesn't allow modification of the permalink after the post is published.

1 Answer 1

0

One thing you can do is pre-set the post_name field using the wp_insert_post_data hook. The field passes 2 variables: $data which is the initial default data and $postedarr which is what's given whenever the post is $_POSTed, updated, or published.

/**
 * Modify the postdata before it's inserted into the database
 *
 * @param Array $data
 * @param Array $postarr
 *
 * @return void
 */
function wpse345850_modify_postdata( $data, $postarr ) {

    if( 'post' != $data['post_type'] || $postarr['ID'] ) {  // A $_POSTed ID means it's an update, not new
        return $data;
    }

    $data['post_name'] = date( 'Y-m-d' );

    return $data;

}
add_filter( 'wp_insert_post_data', 'wpse345850_modify_postdata', 10, 2 );

Note that you'll have to change the above post type from post to your desired post type.

3
  • 1
    Wowww, thank you kind sir! That worked perfectly :D
    – McDeeez
    Commented Aug 26, 2019 at 3:36
  • Hey, i'm actually running into an issue with this that i'm wondering if you can help me with. Because the post_name is getting updated in the DB upon new post, if i start to make a post on say, Sept 9 and don't actually publish it, then try and create a new page later on to give it the 2019-09-09 url, wordpress sees it as if that permalink is already taken and appends a -2 to it. I've deleted every draft and permanently deleted everything in my trash, yet it still won't let me take the 2019-09-09 permalink...even looked in the database and there are no posts occupying the name 2019-09-09
    – McDeeez
    Commented Sep 9, 2019 at 18:08
  • @McDeeez I can't replicate the issue you describe with posts or pages. There has to be something that it find in the database which is telling your post that the slug is duplicate. If it's not in the posts table ( check under slug ) maybe check the post meta table for values. Pages sometimes use _wp_old_slug to handle redirects of renamed pages.
    – Howdy_McGee
    Commented Sep 9, 2019 at 18:14

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.