Docs    YouTube    Documentation

Add Category Support for the YouTube plugin Custom Post Type

The following document goes through additions to your theme’s functions.php file. Only make changes to this at your own risk and ensure you have a backup of your site ready. The code below has been tested and works. Keep in mind that after any changes to your theme’s functions.php file, changing or updating your theme can remove these changes. It’s recommended to use a child theme’s functions.php to ensure these changes remain.

The following code snippet will allow you to add support for categorizing the videos that are pulled into your site and converted into custom post types. The following needs to be added to the functions.php file for your currently activated theme.

function sby_add_category_support() {
    register_taxonomy_for_object_type( 'category', 'sby_videos' );
    register_taxonomy_for_object_type( 'post_tag', 'sby_videos' );
}
add_action( 'init', 'sby_add_category_support' );

Please note, the categories that you create for these videos need to be unique and shouldn’t be the same as your normal post categories. For example, if you have a fishing website and you have a post category named “Fly Fishing” you should set the videos related to this category to something like “Fly Fishing Videos”. This will ensure the WordPress database doesn’t cause issues.

In addition to the above, if you want the videos to show in the specific Category and Tag pages, you will in addition need to add the following to the theme’s functions.php file.

function sby_add_cpt_category_tag ( $query ) {
	if ( is_admin() || ! $query->is_main_query() ) {
		return;    
	}

	if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
		$sby_post_types = array( 'sby_videos' );
		$query->set(
	  		'post_type',
			array_merge(
				array( 'post' ),
				$sby_post_types
			)
		);
	}
}
add_filter( 'pre_get_posts', 'sby_add_cpt_category_tag' );

After the above changes, you may need to remove all existing video custom posts, and import them again either by clearing your feed’s cache or using the import tool. Then the Categories and Tags will be available.

support

Couldn’t find your answer in the docs?

Contact Support

Was this article helpful?