Forked from petenelson/custom-permalink-from-meta.php
Created
March 16, 2020 14:29
-
-
Save Asikur22/0a4d071c84df00a25e2ae95cc5587751 to your computer and use it in GitHub Desktop.
WordPress permalink for custom post type based on post meta
This file contains hidden or 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