Im trying to get an author as slug for my designers website. I will give a example below of what im trying to do:

I have a designer named "Designer A" with nicename "designer-a". I have an art object (custom post-type) named "Awesome Art" with nicename "awesome-art".

I want to achieve that the URL of the art object will be "http://domain.com/designer-a/awesome-art/"

To achieve this i added a rewrite to my custom post-type as followed:

    'rewrite' => array(
        'slug' => '/%author%/',
    ),

And added a filter as followed:

add_filter( 'post_type_link', 'art_post_type_link', 10, 4 );
function art_post_type_link( $post_link, $post, $leavename, $sample )
{
    if ( 'art' == $post->post_type ) {
        $authordata = get_userdata( $post->post_author );
        $author = $authordata->user_nicename;
        $post_link = str_replace( '%author%', $author, $post_link );
    }

    return $post_link;
}

This does work but now i am unable to access my normal pages and im getting a 404 on them. So this isn't the solution i want. So my question is, is the thing i want even possible, and if so what am i doing wrong?

Would love some input!

up vote 0 down vote accepted

Alright after some more intensive research and A LOT of errors i got a fix which seems legit.

First of all i keep the rewrite rule in my custom post-type but did change the name:

'rewrite' => array(
    'slug' => '/%designer%/',
),

Then i need to point the %designer% to the correct field (which was in my case a custom field made by ACF. I did this as followed:

add_filter( 'post_type_link', 'art_post_type_link', 10, 4 );

function art_post_type_link( $post_link, $post, $leavename, $sample )
{
    if ( 'art' == $post->post_type ) {
        $designer = get_field( 'designer', $post->ID );
        $post_link = str_replace( '%designer%', $designer['user_nicename'], $post_link );
    }

    return $post_link;
}

Then i needed to make sure the designer/author had his own URL without /author/ base. And that /%author%/%art-name% works. I did this with the following author rewrite.

add_filter( 'author_rewrite_rules', 'no_author_base_rewrite_rules' );

function no_author_base_rewrite_rules( $author_rewrite ) {
    global $wpdb;
    $author_rewrite = array();
    $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");

    foreach($authors as $author) {
        $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
        $author_rewrite["({$author->nicename})/([^/]+)/?$"] = 'index.php?post_type=art&name=$matches[2]';
    }

    return $author_rewrite;
}

Then i needed to disable the author_base in the global $wp_rewrite if not in admin:

if( !is_admin() ) {
    add_action('init', 'author_rewrite_base_null');
}

function author_rewrite_base_null() {
    global $wp_rewrite;
    if( 'author' == $wp_rewrite->author_base ) $wp_rewrite->author_base = null;
}

And this did the trick for me, hope it helps someone else.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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