Custom field values in permalink

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.

Leave a Comment