How to create custom URLs for Custom Post Types
-
We have various custom post types on a site and we’re trying to customize the URLs for such posts. Some example post URLs:
current: /core-principles/?core_principle=88
desired: /core-principles/2-respect-for-human-rightscurrent: /contents-of-the-framework/free-prior-and-informed-consent/?guidance_topic=0
desired: /contents-of-the-framework/free-prior-and-informed-consent/1-fpic-definition-and-overviewSo the desired URL format is:
/name-of-post-type/name-of-post/name-of-topic-if-present
Oddly, when viewing a post in WP admin, the permalink is listed correctly but the full page content does not load properly.
—
In our
post-types
directory, this is inprinciples.php
(governing the first example post above):<?php /** * Principles */ namespace App; add_action( 'init', '\App\principles_post_type' ); function principles_post_type() { register_post_type( 'principle', array( 'labels' => array( 'name' => __( 'Core Principles', TEXT_DOMAIN ), 'singular_name' => __( 'Core Principle', TEXT_DOMAIN ), 'add_new' => __( 'New Principle', TEXT_DOMAIN ), 'add_new_item' => __( 'Add New Principle', TEXT_DOMAIN ), 'new_item' => __( 'New Principle', TEXT_DOMAIN ), 'edit_item' => __( 'Edit Principle', TEXT_DOMAIN ), 'view_item' => __( 'View Principle', TEXT_DOMAIN ), 'all_items' => __( 'All Principles', TEXT_DOMAIN ), 'search_items' => __( 'Search Principles', TEXT_DOMAIN ) ), 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'hierarchical' => false, 'rewrite' => array( 'slug' => __( 'core-principles', TEXT_DOMAIN ), 'with_front' => false ), 'show_in_menu' => true, 'show_in_admin_bar' => true, 'supports' => array( 'title', 'revisions' ), 'menu_position' => 6, 'menu_icon' => 'dashicons-thumbs-up' ) ); }
and this is in
guidance.php
(governing the second example post above):<?php /** * Guidances */ namespace App; add_action('init', '\App\guidance_post_type'); function guidance_post_type() { register_post_type( 'guidance', array( 'labels' => array( 'name' => __('Operational Guidance', TEXT_DOMAIN), 'singular_name' => __('Operational Guidance', TEXT_DOMAIN), 'add_new' => __('New Operational Guidance', TEXT_DOMAIN), 'add_new_item' => __('Add New Operational Guidance', TEXT_DOMAIN), 'new_item' => __('New Opertional Guidance', TEXT_DOMAIN), 'edit_item' => __('Edit Operational Guidance', TEXT_DOMAIN), 'view_item' => __('View Operational Guidance', TEXT_DOMAIN), 'all_items' => __('All Operational Guidance', TEXT_DOMAIN), 'search_items' => __('Search Operational Guidance', TEXT_DOMAIN) ), 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'hierarchical' => false, 'rewrite' => array('slug' => __('contents-of-the-framework', TEXT_DOMAIN ), 'with_front' => false), 'show_in_menu' => true, 'show_in_admin_bar' => true, 'supports' => array('title', 'revisions'), 'menu_position' => 5, 'menu_icon' => 'dashicons-analytics' ) ); }
The permalinks are set to “Post name”: /sample-post/
I also tried adding a rewrite rule in
principles.php
:function core_principle_rewrite_tag_rule() { add_rewrite_tag( '%core_principle%', '([^&]+)' ); add_rewrite_rule( '^core_principles/([^/]*)/?', 'index.php?core_principle=$matches[1]','top' ); } add_action('init', 'core_principle_rewrite_tag_rule', 10, 0);
But no dice. How can I set all these custom post type URLs to follow the desired format? Do I need to adjust that rewrite rule or is there some existing setting that should be changed or something?
I’m using WordPress Version 5.2.4 with the Sage starter theme: https://roots.io/sage/
Thanks.
- This topic was modified 5 years, 1 month ago by .
- This topic was modified 5 years, 1 month ago by .
- This topic was modified 5 years, 1 month ago by .
- This topic was modified 5 years, 1 month ago by .
- This topic was modified 5 years, 1 month ago by .
- This topic was modified 5 years ago by . Reason: Moved to Fixing WordPress, this is not a Developing with WordPress topic
The page I need help with: [log in to see the link]
- The topic ‘How to create custom URLs for Custom Post Types’ is closed to new replies.