3

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.

  • post post type settings are controlled on the Settings > Permalinks admin screen. – Milo Jun 5 '18 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. – Mihaela Jun 6 '18 at 12:25
  • 3
    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 '18 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. – Mihaela Jun 6 '18 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 '18 at 3:36
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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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