Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Rerwrite permalink based on ACF radio button selection WordPress


I have a custom post type called "News". There are 4 different "types" of news, but the client only wants a single custom post type. To manage this, I am using the ACF (Advanced Custom Fields) plugin's conditional fields so different fields are visible/toggled when a radio button is selected. Each radio button represents a news type.

The problem is that the permalink structure is going to always be /news/[post-title] and I would like the permalink structure to be "/news/[news-type]/[news-title]" or perhaps just "/[news-type]/[post-title]".

I am looking for a method to target which news-type radio button is selected on save, then write that to the slug. Is this possible?

Answers (3)


Rempty answers:

Hello David
You can try something like this
Add this line to the array of the args declaring the post_type
'rewrite' => array('slug' => 'news/%news_type%');

//Add this function to your functions.php
//Check the comments in the code to replace the field name with the correct one.
function rem_news_rewrite( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
//Check the correct post type

//Here you need to get the value of the field, you have the ID of the post $post->ID and assign the value to $news_type variable
if( $news_type ){
return str_replace( '%news_type%' , $news_type , $post_link );
return $post_link;
add_filter( 'post_type_link', 'rem_news_rewrite', 1, 3 );

After this, flush the permalinks and create a new "news" to test

David Holtz comments:

Let me see how far I get with this.

David Holtz comments:

Dude! I think this works!!! Let me do a little more testing.

David Holtz comments:

Ok. Here are my findings. There are 5 radio buttons that all use the same field name. When I add a new post, I add the title. Sometimes the permalink gets written and shows up right away, and sometimes it doesn't. If it does, no matter what radio button I choose, the permalink will select the label related to the first one. If it doesn't, it is able to wait to see if I change my selection from the first to another, and it will write the correct one (this happens maybe 1/3 of the time).

The good news is that after I click "Save Draft" or "Publish", it totally re-writes it. It does this both before the first time I would click one of these, or even after it's been saved.

The bad news is that while I can always preview the post in Draft mode, it is always a 404 once I hit Publish.

Rempty comments:

Sorry forgot to add the permalink rules (this will solve the 404)
Add this code to your functions.php

add_filter('rewrite_rules_array', 'rem_news_rewrite_rules');
function rem_news_rewrite_rules($rules) {
$newRules = array();
$newRules['news/(.+)/(.+?)/?$'] = 'index.php?news=$matches[2]';
return array_merge($newRules, $rules);

//Dont forget reflush permalinks settings again

About your problem with permalink, this happen because your post is not published and the value selected in the Radio Button is not saved, after save the post the permalink change to the correct value.

David Holtz comments:

Should this come after or before the other functions or does it not matter?

Rempty comments:

After the other functions.


Kyle answers:

What are you using to declare the custom post type? That should have something for adding "rewrite rules".

If it doesn't you'll have to declare/register the post type yourself manually and add a rewrite rule.

David Holtz comments:

I am registering the custom post types manually and using ACF for the fields.

Kyle comments:

Okay, I can write something there if you want to post the custom meta field. It is late here so I'd have to do it in the morning. Someone else may post the rest of my solution before then.

It will look something like this where you put in the tag that gets filtered out like %news_type% and replaced in the url structure dynamically.

You'll also need to flush your permalink stuff once we add the new rule to reset the existing ones.

David Holtz comments:

Thank you. I will review the post you referenced.

May I note that as far as ACF is concerned, it uses a field name in it's UI for the array of radio buttons. In my case, it's called "news_type". Of course, in the code, each radio button's input tag has a 'name' attribute with a value such as "field_58619925792a1".

It's not clear to me if we can get conditional with which radio is selected so that "if 'field_58619925792a1' is 'checked' rewrite the permalink this way else..." or if there's another method.

Kyle comments:

ACF actually stores some things twice (because of what you just posted). It originally was written with just the value you defined, but as it grew people needed a more "programatic" way to access field values, so it does also store it in the normal key as well.


Krishna Tiwari answers:

Hope you are doing well.
Could you share custom post type plugin (files & floder)


David Holtz comments:

As I mentioned in the above thread, the custom post type is registered manually.