1

I’ve set up a custom-post-type on a site for jobs, and this all works fine. When I click on the author (my name) on the Custom Post Type though it goes to the archive.php file (as you can see in the 3rd image).

I’ve set it up so that when when you click on the category shown in the first photo, this goes to a custom taxonomy archive template file taxonomy-job_type.php. There doesn’t seem to be an author-cpt.php template file though in the template hierarchy?

How do I set it so when someone clicks the author of a custom-post-type it shows that author’s CPT posts only?

I’ve included the code from my function.php file and the images of what is happening. Any help would be awesome.

function job_post_types() {
    register_post_type('job', array(
            'rewrite' => array('slug' => 'jobs'),
            'menu_icon' => 'dashicons-id-alt',
            'has_archive' => true,
            'public' => true,
            'publicly_queryable' => true,
            'supports' => array('title', 'editor', 'author', 'excerpt', 'custom-fields'),
            'taxonomies' => array('job_type'), // we have used this from the taxonomies below. Default entry should be ('category', 'post_tag)

            'labels' => array(
            'name' => 'Jobs',
            'add_new_item' => 'Add New Job',
            'edit_item' => 'Edit Job',
            'all_items' => 'All Jobs',
            'singular_name' => 'Job'
            ),
        ));
    }

add_action('init', 'job_post_types');


function job_type_taxonomy(){

    $labels = array(
        'name'              => 'Job Types',
        'singular_name'     => 'Job Type',
        'search_items'      => 'Search Job Types',
        'all_items'         => 'All Job Types',
        'parent_item'       => 'Parent Job Type',
        'parent_item_colon' => 'Parent Job Type:',
        'edit_item'         => 'Edit Job Type',
        'update_item'       => 'Update Job Type',
        'add_new_item'      => 'Add New Job Type',
        'new_item_name'     => 'New Job Type Name',
        'menu_name'         => 'Job Type',
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'job_type' ),
    );

    register_taxonomy( 'job_type', array( 'job' ), $args );
}

    add_action('init', 'job_type_taxonomy');

Any help would be great

CPT with my author name 'Paul' in the meta enter image description here

After clicking my name I go to the main blog index archive page for my name enter image description here

Show Current Template File plugin confirms archive.php is the template file being outputted. enter image description here

  • I found this add_filter function to do the trick for me. It filters the get_author_link through the custom post type, and returns the default archive page for the CPT, so it doesn't return the author archive page (which may work well for me, as it did for me.) Check it out. – tommycopeland May 8 '18 at 11:49
0

When you click on an author link generated in the normal way, the server will not know that you only want the custom posts from this author returned. So you will have to tell the server. This is perfectly doable, but a bit too much work to write it down here completely for you. Here are the steps roughly:

  1. Use the filter author_link to attach the string "?post_type=your-cpt-name" to the link generated on cpt posts. When clicked this will tell the server that you want only cpt posts on this author archive page.

  2. Use the filter pre_get_posts to modify the default author archive query. Inside the function for this filter you can use get_query_var to retrieve the value of post_type and restrict the query to only cpt's (this might even happen automatically, though I'm not sure about that)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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