url rewriting - Change author base and slug in author link

admin2025-06-03  5

Hello im trying to change the author link from mysite/author/username to mysite/companies/dataFromUserMetabox

i tried the approach from this answer chage author url

its working the url changed but the problem is when i try to get the author id in author.php template

if ( $author_id = get_query_var( 'author' ) ) { 
  var_dump($author_id);
 } else {
    echo 'no author id';
 }

the link mysite/companies/dataFromUserMetabox return false

but the link mysite/companies/username return the author id

the default author slug even with the new author base "companies" is working but with the new slug its not, so im guessing there nothing wrong with changing the base from author to companies

Hello im trying to change the author link from mysite/author/username to mysite/companies/dataFromUserMetabox

i tried the approach from this answer chage author url

its working the url changed but the problem is when i try to get the author id in author.php template

if ( $author_id = get_query_var( 'author' ) ) { 
  var_dump($author_id);
 } else {
    echo 'no author id';
 }

the link mysite/companies/dataFromUserMetabox return false

but the link mysite/companies/username return the author id

the default author slug even with the new author base "companies" is working but with the new slug its not, so im guessing there nothing wrong with changing the base from author to companies

Share Improve this question edited Feb 20, 2019 at 9:04 Mohamed Mokhtar asked Feb 20, 2019 at 8:43 Mohamed MokhtarMohamed Mokhtar 591 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

i found out what i was doing wrong, i used this part like i mentioned

add_filter( 'request', 'wpse5742_request' );
function wpse5742_request( $query_vars )
{
if ( array_key_exists( 'author_name', $query_vars ) ) {
    global $wpdb;
    $author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) );
    if ( $author_id ) {
        $query_vars['author'] = $author_id;
        unset( $query_vars['author_name'] );    
    }
}
return $query_vars;
}

and changed the meta_key="nickname" in $author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) ); with my custom metabox meta key this managed to find the author id and add it to the $query_vars successfully

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748886072a314538.html

最新回复(0)