CPT WITH TAXONOMY

PHOTO EMBED

Fri Jul 09 2021 06:51:40 GMT+0000 (UTC)

Saved by @deveseospace #php

/*
* Creating a function to create our CPT
*/


function custom_post_type() {

	// Set UI labels for Custom Post Type
	$labels = array(
		'name'                => _x( 'Recipes', 'Post Type General Name', 'twentythirteen' ),
		'singular_name'       => _x( 'Recipe', 'Post Type Singular Name', 'twentythirteen' ),
		'menu_name'           => __( 'Recipes', 'twentythirteen' ),
		'parent_item_colon'   => __( 'Parent Recipe', 'twentythirteen' ),
		'all_items'           => __( 'All Recipes', 'twentythirteen' ),
		'view_item'           => __( 'View Recipe', 'twentythirteen' ),
		'add_new_item'        => __( 'Add New Recipe', 'twentythirteen' ),
		'add_new'             => __( 'Add New', 'twentythirteen' ),
		'edit_item'           => __( 'Edit Recipe', 'twentythirteen' ),
		'update_item'         => __( 'Update Recipe', 'twentythirteen' ),
		'search_items'        => __( 'Search Recipe', 'twentythirteen' ),
		'not_found'           => __( 'Not Found', 'twentythirteen' ),
		'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
	);

	// Set other options for Custom Post Type

	$args = array(
		'label'               => __( 'Recipes', 'twentythirteen' ),
		'description'         => __( 'Recipe news and reviews', 'twentythirteen' ),
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
		'show_in_rest'        => true,
		'yarpp_support' 	  => true,

		// // This is where we add taxonomies to our CPT
		// 'taxonomies'          => array( 'category' ),
	);

	// Registering your Custom Post Type
	register_post_type( 'Recipes', $args );
}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );






/**
 * Create a taxonomy
 *
 * @uses  Inserts new taxonomy object into the list
 * @uses  Adds query vars
 *
 * @param string  Name of taxonomy object
 * @param array|string  Name of the object type for the taxonomy object.
 * @param array|string  Taxonomy arguments
 * @return null|WP_Error WP_Error if errors, otherwise null.
 */
function cs_recipe_taxonomy() {

	$labels = array(
		'name'					=> _x( 'Categories', 'Taxonomy Categories', 'text-domain' ),
		'singular_name'			=> _x( 'Category', 'Taxonomy Category', 'text-domain' ),
		'search_items'			=> __( 'Search Categories', 'text-domain' ),
		'popular_items'			=> __( 'Popular Categories', 'text-domain' ),
		'all_items'				=> __( 'All Categories', 'text-domain' ),
		'parent_item'			=> __( 'Parent Category', 'text-domain' ),
		'parent_item_colon'		=> __( 'Parent Category', 'text-domain' ),
		'edit_item'				=> __( 'Edit Category', 'text-domain' ),
		'update_item'			=> __( 'Update Category', 'text-domain' ),
		'add_new_item'			=> __( 'Add New Category', 'text-domain' ),
		'new_item_name'			=> __( 'New Category Name', 'text-domain' ),
		'add_or_remove_items'	=> __( 'Add or remove Categories', 'text-domain' ),
		'choose_from_most_used'	=> __( 'Choose from most used text-domain', 'text-domain' ),
		'menu_name'				=> __( 'Category', 'text-domain' ),
	);

	$args = array(
		'labels'            => $labels,
		'public'            => true,
		'show_in_nav_menus' => true,
		'show_admin_column' => false,
		'hierarchical'      => true,
		'show_tagcloud'     => true,
		'show_ui'           => true,
		'query_var'         => true,
		'rewrite'           => true,
		'query_var'         => true,
		'capabilities'      => array(),
		'yarpp_support' 	=> true,
	);

	register_taxonomy( 'recipes-category', array( 'recipes' ), $args );
}

add_action( 'init', 'cs_recipe_taxonomy' );



add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
	if( is_category() ) {
		$post_type = get_query_var('post_type');
		if($post_type)
			$post_type = $post_type;
		else
			$post_type = array('nav_menu_item', 'post', 'recipes'); // don't forget nav_menu_item to allow menus to work!
		$query->set('post_type',$post_type);
		return $query;
	}
}
content_copyCOPY