plugin development - WordPress - Custom permalinks for advanced custom fields (ACF) using post type taxonomy

admin2025-01-07  4

I am trying to update the URL structure for posts within a custom post type. I would like the url to use the taxonomy slug from the "resource-topic" taxonomy, and not the custom post type key.

Currently the custom post type key is used in the permalink.

For example:

  • instead of /resource-new/[blog-title]
  • I want it to be /press-releases/[blog-title]

We are using the advanced custom fields plugin > custom post type and taxonomies.

The custom post type is called Resources (resource-new).

I have attempted to modify this using the ACF advanced settings > URLS, but this does not work.

Thank you

I am trying to update the URL structure for posts within a custom post type. I would like the url to use the taxonomy slug from the "resource-topic" taxonomy, and not the custom post type key.

Currently the custom post type key is used in the permalink.

For example:

  • instead of /resource-new/[blog-title]
  • I want it to be /press-releases/[blog-title]

We are using the advanced custom fields plugin > custom post type and taxonomies.

The custom post type is called Resources (resource-new).

I have attempted to modify this using the ACF advanced settings > URLS, but this does not work.

Thank you

Share Improve this question asked Jul 12, 2023 at 22:01 user2748363user2748363 111 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I'm having a similar problem. No matter what I do, I can't get the custom taxonomies I create in ACF to be part of the custom post type's permalink; it always remains /custom_post_type_slug/post_title

I found a solution that worked for me. The custom taxonomy name I used was "resource-topic" and the post type "resource_new". This function rewrites the URLs and uses the custom taxonomy slug. If you replace these with what you named each and add the function to the functions file it should work for you.

If you have multiple custom post types that need to be rewritten this code would need to be amended.

function custom_rewrite_rules() {
$resource_topics = get_terms([
    'taxonomy' => 'resource-topic',
    'hide_empty' => false,
    'fields' => 'slugs',
]);

if (!empty($resource_topics) && !is_wp_error($resource_topics)) {
    foreach ($resource_topics as $topic_slug) {
        add_rewrite_rule("^{$topic_slug}/([^/]+)/?$", 'index.php?resource-topic=' . $topic_slug . '&resource_new=$matches[1]', 'top');
    }
  }
}

add_action('init', 'custom_rewrite_rules');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262886a848.html

最新回复(0)