Skip to content

Instantly share code, notes, and snippets.

@betojsx
Last active July 14, 2022 15:46
Show Gist options
  • Save betojsx/4ad7eb744a20b7599a66ff806fb77ee8 to your computer and use it in GitHub Desktop.
Save betojsx/4ad7eb744a20b7599a66ff806fb77ee8 to your computer and use it in GitHub Desktop.
Display Custom Post Type in Author Archive WordPress
// In functions.php, add:
<?php
function post_types_author_archives($query) {
if ($query->is_author)
// Add 'books' CPT and the default 'posts' to display in author's archive
$query->set( 'post_type', array('books', 'posts') );
remove_action( 'pre_get_posts', 'custom_post_author_archive' );
}
add_action('pre_get_posts', 'post_types_author_archives');
?>
// if you want to display different layout for custom post type in the same author’s archive page.
// In author.php
<?php
$id = get_the_ID();
$post_type = get_post_type($id);
if ($post_type == 'books') {
include(locate_template('parts/entry.php'));
}
else {
include(locate_template('parts/entry-blog.php'));
}
?>
@plowzzer
Copy link

change "posts" to "post"

@midnightgamer
Copy link

Thanks dude, Helpfule.

@CleversonFelix
Copy link

Thanks a lot dude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment