Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Last active July 20, 2020 20:41
Show Gist options
  • Save ChrisLTD/ee618317ea2d8c811647156c8a17ab23 to your computer and use it in GitHub Desktop.
Save ChrisLTD/ee618317ea2d8c811647156c8a17ab23 to your computer and use it in GitHub Desktop.
Change slug / permalink of WordPress 'posts' post type
<?php
/**
* Rewrite blog posts so they start with /blog
*/
function change_posts_post_type_args( $args, $post_type ) {
if ( $post_type == "post" ) {
$args['rewrite'] = array(
'slug' => 'blog',
'with_front' => false
);
}
return $args;
}
add_filter( 'register_post_type_args', 'change_posts_post_type_args', 20, 2 );
function update_posts_post_link( $post_link, $id = 0 ) {
$post = get_post( $id );
if( is_object( $post ) && $post->post_type == 'post' ) {
return home_url( '/blog/' . $post->post_name );
}
return $post_link;
}
add_filter( 'post_link', 'update_posts_post_link', 1, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment