Forked from petenelson/custom-permalink-from-meta.php
Created
August 25, 2017 13:11
-
-
Save polashrp/b4c635d2fe8e69ffa708f6c991027fde to your computer and use it in GitHub Desktop.
WordPress permalink for custom post type based on post meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'post_type_link', 'my_post_type_link', 10, 3); | |
function my_post_type_link($permalink, $post, $leavename) { | |
if ($post->post_type == 'my-post-type') { | |
$meta = get_post_meta($post->ID, '_my-post-meta', true); | |
if (isset($meta) && !empty($meta)) | |
$permalink = home_url( "my-friendly-url/" . $meta . "/" . $post->post_name . "/"); | |
} | |
return $permalink; | |
} | |
add_filter( 'rewrite_rules_array', 'my_rewrite_rules_array'); | |
function my_rewrite_rules_array($rules) { | |
$rules = array('my-friendly-url/([^/]*)/([^/]*)/?$' => 'index.php?post_type=my-post-type&name=$matches[2]&meta=$matches[1]') + $rules; | |
return $rules; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment