Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to rewrite CPT-Permalinks the correct way (incorporating meta-box-data)?

It’s a bit simpler to let WordPress handle generating the permastruct when you register your post type. I also recommend against using global variables, there are API functions for most of what you need to accomplish with the rewrite class.

function magazine_issues_rewrite() {

    add_rewrite_tag( '%issuenumber%', '([^&]+)' );
    add_rewrite_tag( '%issueyear%', '([^&]+)' );

    // post type registration, other args removed for clarity
    $args = array(
        'rewrite' => array( 'slug' => 'issue/%issueyear%/%issuenumber%' ),
    );
    register_post_type( 'issue', $args );

}
add_action( 'init', 'magazine_issues_rewrite' );

I’m not sure whether or not that will fix your problem, I haven’t tested your full code.

Related Posts:

  1. Permalinks using event date (year & month) instead of publication date
  2. Query all posts where a meta key does not exist
  3. Can the Next/Prev Post links be ordered by menu order or by a meta key?
  4. what is the correct way to compare dates in a WP query_posts meta_query
  5. Advanced search form with filters for custom taxonomies and custom fields
  6. Can a custom post type have a Parent Page?
  7. Where, When, & How to Properly Flush Rewrite Rules Within the Scope of a Plugin?
  8. Meta query with boolean true/false value
  9. Get post with multiple meta keys and value
  10. Correctly delete posts with meta and attachments [duplicate]
  11. Custom Post Type Permalink / Rewrite not working immediately
  12. Best way to flush_rewrite_rules for custom post type, in a mu-plugins plugin?
  13. Custom rewrite rules for archive page and single post
  14. WordPress Rewrite Rules for Custom Post Type and Taxonomy
  15. CPT Template Not Showing – Getting 404
  16. Custom post type 404s with rewriting even after resetting permalinks
  17. Multiple post types – share same ReWrite slug?
  18. Storing revisions of metadata for custom post type
  19. Custom field values deleted when trashing custom post type
  20. How to get all custom fields of any post type
  21. Update CPT meta data using REST API
  22. Rewrite rule for Custom post type Monthly and Yearly archive
  23. Admin Area Custom Type Search By Meta Fields Without Title & Content
  24. add_rewrite_endpoint() not working for custom post type archives
  25. Echo all meta keys of a custom-post TYPE
  26. Custom Post type – how to get checkbox to update meta field to null
  27. How can I filter posts by post_parent in the admin?
  28. Permalink rewrite 404 conflict- WordPress Taxonomies/ CPT
  29. How do I Paginate Search Results for Custom Post Types?
  30. Ordering posts having multiple post-meta date fields
  31. How to modify URL structures in custom post types and taxonomies or terms
  32. Adding meta values to permalink
  33. Custom Post Type with Input fields to seperate table on database.
  34. Archive page with multiple taxonomies rewrite
  35. How do I remove all the metaboxes for a custom post type?
  36. Modify custom post type rewrite rules in a separate function
  37. Custom Queries: Joining On Meta Values From Two Custom Post Types
  38. Gutenberg how to make attribute to save to meta
  39. Permalinks so that one custom post type appears to be a child of another, not working
  40. Help with a TV series Rewrite structure
  41. Taxonomy archives based on Custom Post Type
  42. How to programmatically create a connection with [Plugin: Posts 2 Posts] on cpt publish?
  43. Post metadata deletes itself
  44. How to create a mini directory in WordPress?
  45. How Can I save multiple records in same meta key?
  46. How to achieve this permalink -> category-name/custom-post-type-name/post-name
  47. Change page /2 to /transcript with a Rewrite
  48. Using %postname% tag with a Custom Permastruct creates 400 Bad Request Errors from the server
  49. Get all posts from custom post type and insert in select input as metabox
  50. WordPress Custom Post Type Category Page
  51. Permalink rewrite with custom post type and custom taxonomy
  52. Detect meta value changes when post is updated (post_updated)
  53. Search everything (posts, pages, tags, cpt, meta)
  54. How to Add Pages Under Custom Post Type URL Structure?
  55. Prepend meta_value to permalink of post
  56. How to check if last uri segment is a custom post type or taxonomy term?
  57. Pagination Doesn’t Work
  58. Rewrite custom post type url’s adding meta box values
  59. Custom Post Type: Set post_title equal to a custom post type field
  60. I can’t set meta_key in my custom post type query
  61. Grossly inefficient wordpress loops!
  62. Compare 3 custom fields and sort by oldest
  63. Multiple parameters in a custom post type url rewrite
  64. Permalinks for CPT breaks permalinks to pages
  65. Priority for Categories, Tags, Taxonomies, Posts, Pages and Custom Posts with same url
  66. WP Rewrite Rules – Custom post type & taxonomy
  67. How Do I Use WP_Query to Run This Database Query as Search Result?
  68. Custom Post Types: pretty search URLs and has_archive
  69. Need help with complex custom post type setup
  70. WordPress Admin Panel search posts with custom post meta values along with title
  71. Custom Post Type pagination when CPT ‘rewrite’ rule and a page have the same slug
  72. WordPress monthly archive links result in 404
  73. Creating a custom post type upon registration
  74. How to Update post status using meta data in Custom post TYpe
  75. Remove base slug in CPT & CT, use CT in permalink
  76. Rest Api v2 orderby meta_key in custom post type
  77. bulk Update post_meta with default values
  78. Rewrite rules for custom post type ‘attached’ to another custom post type
  79. Reason action hook won’t work with update_post_meta from frontend form? Alternative?
  80. Can’t sort order of wp_query with 2 meta keys
  81. Showing Meta Data for Custom Post Types?
  82. How to get source of custom meta image?
  83. wrong template for page of archive
  84. Having trouble with custom post type / meta box
  85. Query for posts from any post type but only add instock products
  86. How can I include meta box content when searching?
  87. update a post meta from a single table cell TablePress
  88. update custom post type meta from a shortcode
  89. Custom Meta Boxes – Nonce Issue – Move to trash issue
  90. WordPress Rewrite rule for nested post types
  91. Displaying Meta Box Image
  92. Custom Post Type rewrite redirects to homepage
  93. Custom post type category not displaying in custom post type
  94. How to make custom post meta wp_editor translatable?
  95. Automatically adding meta data to posts or multiple query help
  96. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  97. How to add multiple featured image in meta box in post editor?
  98. Show metabox in custom-post-template depending on taxonomy term?
  99. Disabling post_type in URL
  100. single-{post_type}.php is not loaded despite flushing rules
Categories custom-post-types Tags custom-post-types, post-meta, rewrite-rules
optimise SQL wordpress call
Display posts containing at least 1 from a list of say 10 tags but not containing other tags

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress