StackTips

How to Include WordPress Custom Post Types to Archive Page

stacktips avtar
Editorial   |   #Blog Posts#Wordpress   |   Sep 17, 2023, 

The default WordPress author archive page template displays only default post types. The custom posts are not included. The following WordPress code snippet will help you to include WordPress custom post types to author archive page.

Add the following snippets to your WordPress theme functions.php file:

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {

	if ( is_author() && $query->is_main_query()) {
		$query->set( 'post_type', array( 'post', 'post_type1', 'post_type2' ) );
	}
	return $query;
}

Save the changed functions.php and visit your your WordPress author archive.

stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.