Custom field values in permalink

Is there a way to add a custom field value (if it exists) to the permalink?

Related posts

Leave a Reply

2 comments

  1. Here’s how I’d do it. I’m just going to say, I haven’t tested this code at all, so I don’t know if it even works. It’s more of a starting point for you.

    function jpb_custom_meta_permalink( $link, $post ){
      $post_meta = get_post_meta( $post->ID, '<insert your meta key here>', true );
      if( empty( $post_meta ) || !is_string( $post_meta ) )
        $post_meta = '<insert your default value (could be an empty string) here>';
      $link = str_replace( '!!custom_field_placeholder!!', $post_meta, $link );
      return $link;
    }
    
    add_filter( 'post_link', 'jpb_custom_meta_permalink', 10, 2 );
    

    You would also have to go to your permalinks and change them to include '!!custom_field_placeholder!!', like so:

    '/%year%/%monthnum%/!!custom_field_placeholder!!/%postname%/'
    

    This will not add any significance to your permalinks, merely text. If you need to add meaning to the text that goes there, that’s a lot more work.

  2. Definitely no easy way since it’s not part of Structure tags available for use in pretty permalinks setup.

    For generic post it is probably easiest to edit slug manually.

    Do you expect these posts to be generated by code or something?