Custom fields in Permalinks?

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).

Read More

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.

Related posts

1 comment

    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.

Comments are closed.