1

I'm wondering how to add a custom field value to a custom post type's permalink? For example, I have the custom post file ex: cpt-cities.php, which handles registering the custom post type and all of those specifics.

Within that file, I'm trying to set it's permalinks to include one of the 'cities' custom field values. I'm able to set the permalinks using add_permastruct. However I'm not able to get the value of the custom field, and get_post_meta() does not work. This may be due to $post->ID not working (since it's within the custom post type file, and not a post loop).

Anyone know of a way to get the custom field value within a file like this? Is there somewhere else I can declare the add_permastruct, that will allow me to pull in the custom field value?

Any help is greatly appreciated, this is one of the last parts of a pretty extensive project.

3
  1. Use add_rewrite_tag( $tag, $regex ); to register a rewrite tag for the custom field.
  2. Filter post_type_link to replace the rewrite tag. The second argument is the post object, so you have always access to the post ID.

For a sample implementation see my plugin T5 Rewrite. It does exactly that in the class T5_Rewrite_Tag_Custom.

2
  • Great, I'll check this out this evening and will mark as correct if everything works. Thanks for the input! – Daniel Nov 4 '13 at 17:20
  • I ended up going with an example I found previously to this answer, but it used the same concept @toscho explains here, so this has been marked as the correct answer. – Daniel Nov 5 '13 at 14:44

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.