Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
We are looking for a solution like this to populate a custom field in the post editor with a source URL from Feed WordPress (Syndication) plugin. Basically, the Syndication plugin pulls the feed 'source', but sometimes, the feed owner doesn't include the 'source' url, so we want the editor to be able to add a 'source' URL if one does not exist, so that we can consistently display the target URL with the associated post. This code you have posted is for the "permalink," but do you think we can modify to pull the data from the plugin and add it to the custom field? –  Tony Zeoli Jul 27 '11 at 15:18

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.