Actually, you have used the code you shared wrongly.
$ushub = array('post_type' => 'user-will',
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'wpcf-user-id',
'value' => $current_user->ID
),
),
);
$user_wills= get_posts($ushub);
$user_will_url=get_permalink($user_wills[0]);
I've wrapped the above code with the function as given under:
/* Return User Will URL */
function get_current_will_url(){
global $current_user;
$ushub = array('post_type' => 'user-will',
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'wpcf-user-id',
'value' => $current_user->ID
),
),
);
$user_wills= get_posts($ushub);
$user_will_url=get_permalink($user_wills[0]);
return $user_will_url;
}
Now, whereever you are trying to use the $user_will_url, you should change that to: get_current_will_url();
so, I've changed the wp_nav_menu_items hook as given under:
/* Add User Hub Menu Item */
function custom_add_userhub_link( $items, $args ) {
global $user_will_url;
if ($args->theme_location == 'primary'){
if (is_user_logged_in()) {
// $items .= '<li><a href="' . $user_will_url . '">User Hub</a></li>';
$items .= '<li><a href="' . get_current_will_url() . '">User Hub</a></li>';
} else {
$items .= '<li><a href="<em><u>versteckter Link</u></em>">Log In</a></li>';
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'custom_add_userhub_link', 10, 2 );
Now, when I submit the new entry using the form: versteckter Link
I see the URL stays at: versteckter Link
Can you please confirm it works at your end as well.