Adding a Default Category to a WordPress Custom Post

I have been searching for a way to add a default category into a WordPress Custom Post and finally got to a method that seemed to work for me. The explanation I came across was a bit vague so I thought I would try and clear it up here: –

The Code

// Adds the category to the jobs post automatically

function add_jobs_category_automatically($post_ID) {
global $wpdb;
if(!has_term(”,’category’,$post_ID)){
$cat = array(139);
wp_set_object_terms($post_ID, $cat, ‘category’);
}
}
add_action(‘publish_job’, ‘add_jobs_category_automatically’);

Explanation

line 1: function add_jobs_category_automatically($post_ID) {

This just sets the name of the function, in this case “add_jobs_category_automatically” declares it as a function and says what it applies to.

CHANGE ‘add_jobs_category_automatically’ to anything that will make sense to you.


 

line 2: global $wpdb;

Ok, I’m guessing at some of these, but I would say that this means that the function applies globally to the wordpress database

CHANGE: Nothing


 

line 3: if(!has_term(”,’category’,$post_ID)){

Some WordPress gubbins!

CHANGE: Nothing


 

line 4: $cat = array(139);

This is the category number you want to apply to the custom post. In my case it was 139, but yours could be anything.

To find the category number in your WordPress admin interface, go to Posts, Categories and mouse over the category you want to add automatically. You should see a number under category&tag_ID=139, the number is what you need (if you can’t see it by mousing over copy the link and paste it somewhere, it should look something like http://www.mysite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=139&post_type=post. Just copy the number and paste it into this line.

CHANGE: The number to your category


 

line 5: wp_set_object_terms($post_ID, $cat, ‘category’);

Some WordPress gubbins

CHANGE: Nothing


 

line 6, 7 just closing brackets
CHANGE: Nothing


 

line 8: add_action(‘publish_job’, ‘add_jobs_category_automatically’);

This is the tricks one, so I will break it into 3 parts

add_action – Some WordPress Gubbins – CHANGE: Nothing

publish_job – This says what your custom post type is called that this should apply to. In my case it is the job in publish_job. You will need to change the job to your custom post type.

To find this go to your custom posts, to the page where they are listed. The URL of the page is key. In my site it is http://www.mysite.com/wp-admin/edit.php?post_type=job. You need what comes after the post_type= in this url. As you can see mine is job.
CHANGE: publish_job, the job to your custom post type.

add_jobs_category_automatically – This is the function you created in line 1, so you should change this to the function name you made there. In my case line 1 was “function add_jobs_category_automatically($post_ID) {“, so you can see that the bit you need is between “function” and “($post_ID)”
CHANGE: All of it to your function name in line 1


 

You need to add your final code into the functions.php file, somewhere before the closing ?> tag. In mine I added it just after the function to create the custom post. So that whole section looks as follows: –

// Add Careers Custom Post Type

add_action( ‘init’, ‘register_cpt_job’ );
function register_cpt_job() {
$labels = array(
‘name’ => _x( ‘Careers’, ‘job’ ),
‘singular_name’ => _x( ‘Job’, ‘job’ ),
‘add_new’ => _x( ‘Add New’, ‘job’ ),
‘add_new_item’ => _x( ‘Add New Job’, ‘job’ ),
‘edit_item’ => _x( ‘Edit Job’, ‘job’ ),
‘new_item’ => _x( ‘New Job’, ‘job’ ),
‘view_item’ => _x( ‘View Job’, ‘job’ ),
‘search_items’ => _x( ‘Search Careers’, ‘job’ ),
‘not_found’ => _x( ‘No careers found’, ‘job’ ),
‘not_found_in_trash’ => _x( ‘No careers found in Trash’, ‘job’ ),
‘parent_item_colon’ => _x( ‘Parent Job:’, ‘job’ ),
‘menu_name’ => _x( ‘Careers’, ‘job’ ),
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => false,
‘description’ => ‘Careers section for displaying jobs on the site.’,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’, ‘custom-fields’, ‘revisions’, ‘page-attributes’ ),
‘taxonomies’ => array( ‘category’, ‘post_tag’ ),
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘show_in_nav_menus’ => true,
‘publicly_queryable’ => true,
‘exclude_from_search’ => false,
‘has_archive’ => true,
‘query_var’ => true,
‘can_export’ => true,
‘rewrite’ => true,
‘capability_type’ => ‘post’
);
register_post_type( ‘job’, $args );
}

// Adds the category to the jobs post automatically

function add_jobs_category_automatically($post_ID) {
global $wpdb;
if(!has_term(”,’category’,$post_ID)){
$cat = array(139);
wp_set_object_terms($post_ID, $cat, ‘category’);
}
}
add_action(‘publish_job’, ‘add_jobs_category_automatically’);

The green text at the top is my custom post type and the blue test at the bottom is the code to set my default category in the custom post.

Leave a Reply

Your email address will not be published.