Home Forums General Issues Add ACF Field to Permalink


Add ACF Field to Permalink

  • I have a custom post type for “events” and I would like to place the event year in the permalink. Is this possible using ACF if I use the datepicker? I do have a rewrite filter for my custom post type “events”, I am not sure if it’s possible to add the field name for the year to that rewrite?

    Below is the code for the filter. Where it currently says ‘%year%’ I would like to replace that with the year of the event, not the year the post was created.

    // add to our plugin init function
    global $wp_rewrite;
    $events_structure = '/%event_type%/%year%/%events%';
    $wp_rewrite->add_rewrite_tag("%events%", '([^/]+)', "events=");
    $wp_rewrite->add_permastruct('events', $events_structure, false);
    // Add filter to plugin init function
    add_filter('post_type_link', 'events_permalink', 10, 3);	
    // Adapted from get_permalink function in wp-includes/link-template.php
    function events_permalink($permalink, $post_id, $leavename) {
    	$post = get_post($post_id);
    	$rewritecode = array(
    		$leavename? '' : '%postname%',
    		$leavename? '' : '%pagename%',
    	if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
    		$unixtime = strtotime($post->post_date);
    		$category = '';
    		if ( strpos($permalink, '%category%') !== false ) {
    			$cats = get_the_category($post->ID);
    			if ( $cats ) {
    				usort($cats, '_usort_terms_by_ID'); // order by ID
    				$category = $cats[0]->slug;
    				if ( $parent = $cats[0]->parent )
    					$category = get_category_parents($parent, false, '/', true) . $category;
    			// show default category in permalinks, without
    			// having to assign it explicitly
    			if ( empty($category) ) {
    				$default_category = get_category( get_option( 'default_category' ) );
    				$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
    		$author = '';
    		if ( strpos($permalink, '%author%') !== false ) {
    			$authordata = get_userdata($post->post_author);
    			$author = $authordata->user_nicename;
    		$date = explode(" ",date('Y m d H i s', $unixtime));
    		$rewritereplace =
    		$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    	} else { // if they're not using the fancy permalink option
    	return $permalink;
  • Did you ever get this to work?

  • I am looking for something similar and I managed partially. I managed to ‘construct’ a new permalink, but it’s not working, it’s forwarding to home.

    I was trying to get the following permalink structure: /post-type/%post_id%-%custom_field_1%-%custom_field_2%

    That I managed with the code below. But the permalink itself forwards to home instead of the post itself.

    If anyone sees an error, or knows the solution, please let me know.

    function register_rewrite_rules()
        global $wp_rewrite;
        $url_structure = '/my_post_type/%post_id%-%cf1%-%cf2%';
        $wp_rewrite->add_permastruct('my_post_type', $url_structure, false);
    function my_custom_permalink($permalink, $post_id, $leavename) {
        $post        = get_post($post_id);
        $dvp_name    = get_field('dvp_name', $post_id);
        $dvp_city    = get_field('dvp_city', $post_id);
        $rewritecode    = array(
        if ( !empty($permalink) && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
            $rewritereplace = array(
            $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
        } else { // if they're not using the fancy permalink option
        return $permalink;
  • @beee

    Did you ever figure this out? I have an identical issue to you and cannot for the life of me figure it out. Desperate for the answer if you have it!


  • @tomhawkins yes I was….

    My raw rewrite file =
    The site is not live hence why there are still commented functions and var_dumps in there. I still need to clean it before going live.

    There are a few ‘adult’ terms in there, just fyi….

  • @beee thanks for the quick reply, pastebin link is great. What was the issue with it redirecting at first for you which was solved through the linked file, not building the url properly? I’ve tried adapting the link you provided to rewrite the permalink on my site, but seem to be struggling.


  • I don’t think I found out… I removed the code and started again from scratch…

    I would be willing to help you take a look at it, but i’m at work now and I’m only available after 20:00 CET (in about 4 hours)

  • @beee fair enough – honestly that would be great, I really don’t know PHP very well but if you are happy to take even a quick glance that would be fantastic. If this is ok with you, what would be the easiest way – pastebin again?

  • I do but currently not on this project as it needs to remain private – would it just be functions.php you’d look at?

  • just use, you can have private projects for free (not like on github), works perfecly. My user is beee4life.

  • will get that sorted, thanks again

  • Hi, I created this support thread over 2 years ago, from my findings there is no way to add a custom post type to the permalink. I am going to close this topic, I am not need to figure out a solution to this issue anymore. Thanks for everyone who replied!

  • you are wrong, there is a way…. and why don’t you just unsubscribe and leave it open for others are looking for a solution ?

  • When I first posted this issue I was told it wasn’t possible to add a custom field to a permalink.

    Maybe you should try this plugin:

    Or create a custom post type and rewrite your permalinks with a custom post type instead of a custom advanced field. But from my experience rewriting permalinks messed up the website/theme I was developing, in my opinion it’s best to use WordPress’s structure for permalinks.

  • sorry to say but you have been misinformed. I have created my own custom permalinks with the custom post type in it as well as acf values.

  • And you haven’t had any issues with archives? Broken links?

  • nope… all works…

    my permalink is domain.tld/post-type/acf_field/title

  • @beee
    Could you share your solution?

    The paste-bin code seems incomplete.


  • It might seem that way (to you) 🙂 but it’s not…

    You need to change the post_link/post_type_link with a filter and then use a rewrite rule.

    The function on line 30 in the pastebin explains it and line 58 shows a rewrite.

    With that you should be able to adapt it to your needs.

Viewing 21 posts - 1 through 21 (of 21 total)

The topic ‘Add ACF Field to Permalink’ is closed to new replies.