Forked from petenelson/custom-permalink-from-meta.php
Created
November 23, 2017 12:52
-
-
Save rvpatel/642aba8d31db6783aa4910f19b324a8e 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