0

For an existing custom_user_meta="user_name" how can i create a permalink for post submitted by that user in the URL.

e.g. - for user_name="srk489" i want the permalink of the post "post1" posted by that user to be "http://localhost/srk489/post1".

To get the user meta i got the following function

get_user_meta($user_id, $key, $single);  

When i searched i got the following code for adding custom post field in permalink

function custom_post_title($title) {
    global $post;
    $type = get_post_type($post->ID);
    if ($type== 'post') {
    $title = $post->post_title;
    $custom_field_title = get_post_meta($post->ID, 'custom_post_title', true);
    if(isset($custom_field_title))
      return $title.'-'.$custom_field_title;
    else
      return $title;

    }
   return $title;
}
add_filter ('title_save_pre','custom_post_title');

How do i relate the user meta and post permalink with each other so that i can get user_meta as permalink?

2
  • Maybe I am missing something. Wouldn't the username already be the "author" Why can't you just use %author%/%postname% as your permalink structure?
    – skribe
    Commented Feb 22, 2015 at 20:10
  • yes that is what i want ....but i case of username....i want a custom_user_meta Commented Feb 23, 2015 at 4:21

1 Answer 1

0

After searching a lot i finally came up with this solution.

My custom post type is "facility" and the custom_user_meta="organization_name".

First go to http://localhost/wp-admin/options-permalink.php and choose custom structure - in the text box put /%organization_name%/%postname%/

After that put the below given code in functions.php

add_action('init', 'tdd_add_rewrite_rules2');
function tdd_add_rewrite_rules2(){
// Register custom rewrite rules
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%facility%', '([^/]+)', 'facility=');
$wp_rewrite->add_rewrite_tag('%organization_name%', '([^/]+)', '');
$wp_rewrite->add_permastruct('facility', '/%organization_name%/facility/%facility%', false);
}

add_filter('post_type_link', 'tdd_permalinks2', 10, 3); 
function tdd_permalinks2($permalink, $post, $leavename){ 
$no_data = get_the_author_meta('ID');;
$post_id = $post->ID;
if($post->post_type != 'facility' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))  return $permalink;
// $var1 = get_post_meta($post_id, 'posts_solicitorspeciality', true);
$var1 = get_the_author_meta('organization_name');

$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace('%organization_name%', $var1, $permalink); 
return $permalink; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.