Category for only one CPT - wordpress

I have created CPT and added a taxonomy category.
The problem is that those categories from CPT appear also in my blog.
I would like to have diffrent categories in my blog and diffrent in my CPT portfolio.
Here is my current code
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'portfolio',
array(
'labels' => array(
'label' => 'portfolio',
'name' => __( 'Portfolio' ),
'singular_name' => __( 'portfolio' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array('category','post_tag'),
'supports' => array( 'title', 'comments', 'excerpt', 'custom-fields', 'thumbnail' )
)
);
};

Quoting the codex,
"Even if you register a taxonomy while creating the post type, you
must still explicitly register and define the taxonomy using
register_taxonomy().
Basically, you're creating a new taxonomy called category for your Portfolio CPT. This will function exclusive of the default category taxonomy of the Posts.

Related

How can i automatically create and save a shortcode for each custom post type when user creates a new custom post type

I have a created custom post type using this code,
function adsManager_custom_post_type(){
register_post_type('adsmanager_banner',
array(
'labels' => array(
'name' => __('Banners', 'textdomain'),
'singular_name' => __('Banner', 'textdomain'),
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-admin-page',
'rewrite' => array( 'slug' => 'banners' ),
'supports' => array(
'title',
'editor',
'short-code'
)
)
);
}
add_action('init', 'adsManager_custom_post_type');
Now I want to automatically add a shortcode for each custom-post-type (banner) when user creates a new banner and then show the shortcode for each banner in banners screen like,
You have plenty of options if you are looking for an hook after the post is saved. They all comes from the same file which is:
wp-includes/post.php
In that file you can find this action and many more:
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
After you hook in that you can have this:
add_action('save_post_mycpt','afterMyCptIsSaved');
function afterMyCptIsSaved($postId,$post,$update){
....
your code
....
}

WordPress: How do I add more than one taxonomies in a custom post post type

I have created a Custom Post Type called user-story. The $args looks like this:
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'description',
'taxonomies' => array('category', 'story-type', 'genre'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'http://webmaster.webmastersuccess.netdna-cdn.com/wp-content/uploads/2015/03/pencil.png',
'public' => true,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'supports' => $supports,
'rewrite' => $rewrite,
'register_meta_box_cb' => 'add_story_metaboxes' );
register_post_type('user_story', $args);
The problem is in line 'taxonomies' => array('category', 'story-type', 'genre'),. I cannot see my taxonomies story-type and genre in Add New Story page in admin. Only category is showing up.
Both story-type and genre are custom taxonomies. I deactivated CPT plugin (user_story) and then reactivated it. But still above custom taxonomies are not coming up.
Both custom taxonomies are registered through plugins and they are visible in Admin menu. Terms registered under these taxonomies are also showing up in their respective list pages.
Screenshot-1: List of terms registered in taxonomy story-type
Screenshot-2: List of terms registered in taxonomy genre
Screenshot-3: Add New Story page - none of the above taxonomies other than only the built-in taxonomy category is listed
I referenced this.
This one should help:
https://developer.wordpress.org/reference/functions/register_taxonomy/
Place this code in your functions.php file and custom taxonomies should be added to your custom post type.
<?php
add_action( 'init', 'create_user_story_tax' );
function create_user_story_tax() {
/* Create Genre Taxonomy */
$args = array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
);
register_taxonomy( 'genre', 'user-story', $args );
/* Create Story Type Taxonomy */
$args = array(
'label' => __( 'Story Type' ),
'rewrite' => array( 'slug' => 'story-type' ),
'hierarchical' => true,
);
register_taxonomy( 'story-type', 'user-story', $args );
}
?>

Wordpress Custom Post Type Template

I am working on a wordpress website. I need to add custom Post types. I have created a custom post type products which has a taxonomy product_type which is similar to category . There are various taxonomy value for product_type . Some of them are flowers, extracts etc .
Now I am trying to visit this link http://farma.mechadigital.net/products/product_type/flowers/ and it doesnt work for me.
I have added some files.
archive-products => This should be the custom post template
taxonomy-product_type.php => This should be the taxonomy Template
taxonomy-product_type-flowers.php => This should be the template for the term value flowers
Here is the code that I have included in functions.php. I dont know where I am doing it wrong.
functions.php
function farma_products() {
$labels = array(
// List of arguments
);
$args = array(
// list of arguments
'rewrite' => array( 'slug' => 'products' ),
);
register_post_type( 'products', $args );
flush_rewrite_rules(false);
}
add_action( 'init', 'farma_products_type' );
function farma_products_type() {
register_taxonomy(
'product_type',
'products',
array(
'label' => __( 'Product Type' ),
'rewrite' => array( 'slug' => 'products/product_type' ),
'hierarchical' => true,
)
);
}

Category Template for custom posts

Hi I would like to use category-(slug).php for my custom post type.
In functions.php I have defined the movies post type as following
add_action( 'init', 'create_post_type_movie' );
function create_post_type_movie() {
register_post_type( 'ra_movie',
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'has_archive' =>true,
'rewrite' => array('slug' => 'movie'),
'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields','revisions'),
'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
));
}
So the slug of teh post type is movie. when I load
../category-movie/design
it displays the 404. I am using wordpress 3.5.1 so it should be able to display category-(slug).php
Any ideas?
Thank you

Move WordPress posts and related taxonomy to custom post type & taxonomy

I have a clean WordPress installation using Twenty Eleven, with no plugins or other modifications. The blog contains approximately 800 posts with related tags and categories.
I'd like to move all these posts to a custom post type. By adding the following code to my functions.php my blog now has the custom post type installed and working perfectly.
//Add custom post type
add_action( 'init', 'create_en_post_type' );
function create_en_post_type() {
register_post_type( 'en_post', array(
'labels' => array(
'name' => __( 'English Posts' ),
'singular_name' => __( 'English Post' )
),
'public' => true,
'menu_position' => 5,
'rewrite' => array('slug' => 'en'),
'supports' => array(
'title',
'editor',
'author',
'excerpt',
'comments',
'custom-fields'
)
));
}
//Add belonging custom taxonomy
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'english_categories', 'en_post', array( 'hierarchical' => true, 'label' => 'English Categories', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'english_tags', 'en_post', array( 'hierarchical' => false, 'label' => 'English Tags', 'query_var' => true, 'rewrite' => true ) );
}
Now all I need is to change the regular post type to my custom post type. I've managed to move the posts using the plugin Convert Post Types, but my taxonomies aren't converted.
I realize I could just create similar custom categories and assign the custom posts to them, but what about tags? How can I convert these (automatically, 1200 tags)?
I've tried manually messing around in the database, but I can't make it work.
You should change the ID's in the database, take a long look at the phpmyadmin
wp_term_taxonomy
wp_terms
wp_term_relationships
In the terms table you will find the id of the old and new taxamonies
in term_taxonomy replace all old term_id with the new.
SQL:
UPDATE `wp_term_relationships` SET `term_id` = REPLACE(`term_id`, /*old id*/, /*new id*/);
Do make a backup before you start

Resources