Toolonomy Logo
Last Updated on: September 18, 2020

Assign Default Taxonomies to Custom Post Types in WordPress [PHP Snippet]

0 Shares

If you want to assign the default blog post taxonomies to your custom post types then you can easily achieve that using this powerful function PHP code snippet.

/* Advanced Scripts Method to Add This Snippet */
/* 
Title: Assign Default Taxonomies to Custom Post Types
Type: PHP: Custom Code
Location: Plugins Loaded
Priority: 10
*/

function my_cptui_add_post_types_to_archives( $query ) {
	// We do not want unintended consequences.
	if ( is_admin() || ! $query->is_main_query() ) {
		return;    
	}

	if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

		// Replace these slugs with the post types you want to include.
		$cptui_post_types = array( 'custom-post-type');

		$query->set(
	  		'post_type',
			array_merge(
				array( 'post' ),
				$cptui_post_types
			)
		);
	}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
0 Shares

Become a Toolonomy Community Member for Free!

Consider joining our Official Community Group if you want to get access to the exclusive insider content and information around the Exclusive Business Development Tools. Also, you will be able to get involved in interesting group discussions with like-minded people that are interested in similar topics as you.
Free Membership
Made with ❤ for Digital Entrepreneurs
Copyright © 2018 - 2022 SyncWin | All Rights Reserved.
Top crossmenu
0 Shares
Copy link
Powered by Social Snap