5

Like many other, I would like to have: domain.com/post-title changed into domain.com/blog/post-title but only for the post type 'post', not for 'page' and especially not for the custom post types (of which my theme seems to have many).

I have done my research on this forum and other sources and I know the general answer seems to be:

When you register your post type, the with_front argument of rewrite should be false.

$args = array(
    'rewrite' => array( 'with_front' => false ),
); 
register_post_type( 'your-post-type', $args );

Unfortunately, this does not help the beginners. We don't know what is meant by the instructions above. Apparently we should somehow re-register the default post type "post" (although the post type "post" already exists and is in use), but we don't know how and where to do that. If anyone can shed some light on the necessary procedure for changing the blog posts URLs, I would be most grateful.

5
  • post post type settings are controlled on the Settings > Permalinks admin screen.
    – Milo
    Jun 5, 2018 at 18:09
  • @Milo Thank you. Indeed, there are some URL settings that can be controlled that way. However, in my particular case though, I only want to change URL setting for posts that have the type 'post'. More precisely, if I go to permalink settings -> Common settings and instead of the current: "Post name" checked option I select 'Custom structure' and write '/blog/%postname%', not only all my blog posts will have the 'blog' word prepended to the URL, but also all my custom post types. This is not the desired outcome. Jun 6, 2018 at 12:25
  • 4
    You’ve already got the answer to that problem, custom types should of be registered with with_front set to false as you’ve described in your question above.
    – Milo
    Jun 6, 2018 at 12:51
  • @Milo Since these custom types are created by the theme developers, we, the beginer wordpress users, do not know what is meant by registering a custom type. Therefore our question refers precisely to what it is that we need to do to register and where. Should we include the mentioned coded in a functions.php file in a child theme that inherits the main theme we use ? In that case, wouldn't this mean we are re-registering a custom type, since it is already registered in the parent theme ? Full disclosure: I have already tried doing the above and it has no effect, their link still changes. Jun 6, 2018 at 20:39
  • If you don't control the code that registers the post type, then there is a filter that lets you alter the arguments without having to re-register the post type. See this question and answer for an example.
    – Milo
    Jun 7, 2018 at 3:36

3 Answers 3

3

I found the answer here. Remember to pop in there and give it a like.

I'll post it here, for people in a rush.


Put this into the functions.php-file:

function wp1482371_custom_post_type_args( $args, $post_type ) {
  if ( $post_type == "post" ) {
    $args['rewrite'] = array(
      'slug' => 'blog'
    );
  }

  return $args;
}
add_filter( 'register_post_type_args', 'wp1482371_custom_post_type_args', 20, 2 );

(Tested and works).

Remember!!

Remember A) Remember to update your permalinks afterwards (by going into Settings >> Permalinks >> Click 'Save Changes' ).

Remember B) If you get wierd results, then try opening an incognito-window and see if it words there. WordPress has a feature that redirects to 'Nearest Matching URL', that can seem confusing, when playing around with permalinks.


You could also try to find a Plugin that does it. I wouldn't do that, since it's quite extensive to add a plugin for that sole purpose. But hey, - sometimes it can be satisfying to shoot birds with canons (no bird was harmed making this joke).

3
  • A short and beautiful solution with a reasonable explanation. Sadly, it's not what I'm looking for. Currently, it uses the "rewrite" definition to redirect to the old permalink ("/blog/post' -> "/post"). However, I want it to keep the URL "/blog/post". How would you achieve that?
    – Chaoste
    Nov 2, 2020 at 13:42
  • 3
    To extend this: One can fix it by adding the following: add_filter('pre_post_link', 'my_change_post_link', 10, 3); function my_change_post_link($permalink, $post, $leavename) { if (get_post_type($post) == 'post') { return "/blog" . $permalink; } return $permalink; }
    – Chaoste
    Nov 2, 2020 at 14:04
  • @Chaoste OMG what are the chances I needed this the very next day. Looked everywhere - TYTY
    – gerbz
    Nov 3, 2020 at 21:25
0

Did you tried this?

function generate_rewrite_rules( $wp_rewrite ) {
      $new_rules = array(
        '(.?.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[2]',
        'blog/([^/]+)/?$' => 'index.php?post_type=post&name=$matches[1]',
        'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]',
        'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1',
        'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]',     
        'blog/[^/]+/attachment/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
        'blog/[^/]+/embed/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
        'blog/([^/]+)/embed/?$' => 'index.php?post_type=post&name=$matches[1]&embed=true',
        'blog/[^/]+/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true',
        'blog/([^/]+)/trackback/?$' => 'index.php?post_type=post&name=$matches[1]&tb=1',
        'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]',
        'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]',
        'blog/page/([0-9]{1,})/?$' => 'index.php?post_type=post&paged=$matches[1]',
        'blog/[^/]+/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]',
        'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]',
        'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&cpage=$matches[2]',
        'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?post_type=post&name=$matches[1]&page=$matches[2]',
        'blog/[^/]+/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]',
        'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1',
        'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]',
      );
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    add_action( 'generate_rewrite_rules', 'generate_rewrite_rules' );

    function update_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_post_link', 1, 3 );
0
//Place code in function.php 
add_action('init', 'my_new_default_post_type', 1); 
function my_new_default_post_type() 
{ 
    register_post_type('post', array(
        // 'labels' => $labels, 
        'public' => true, 
        //'show_in_admin_bar' => true,
        '_builtin' => false, 
        '_edit_link' => 'post.php?post=%d', 
        'capability_type' => 'post', 
        'map_meta_cap' => true, 
        'hierarchical' => false, 
        'rewrite' => array('slug' => 'resources/blog'), // Add your slug here 
        'query_var' => false, 
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats'),
    )); 
    /* If you add above code in function.php, Your blog pagination is stopped to working, You need to add rewrite rule using "Debug This" Plugin under Query >> Rewrite Section & modify your rewrite_rule. */ 
    add_rewrite_rule('(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=resources/blog&paged=$matches[1]', 'top');
}

2
  • Hmm.. I wouldn't do this, since it 're-registers' the post-post type.
    – Zeth
    Feb 26, 2020 at 13:25
  • You have need to change your permalink structure. Feb 27, 2020 at 9:20

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