最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Setting Default Category for Custom Post Type Upon Autosave

programmeradmin1浏览0评论

I've been searching for a couple hours and consulted multiple posts, but I seem to get this to work.

I have this weird bug where users cannot see their drafts of a custom post type if they don't assign a category to them. So if they just click 'save as draft' and want to go back to it later, it won't be visible to them (I have to go in as an admin and set a category for it to be visible to them). I have no idea why this is occurring but I'm willing to work around it.

In Settings > Writing you can set the default category for a regular post, but there is no such option available for a custom post type. I am fine to set the default type to 'Uncategorized', just like it is set with regular posts. So I'm trying to accomplish this.

Some snippets I've come across like this are aimed for 'default category upon publish' but I need it upon autosave (some users just have access to 'save draft' and 'submit for publishing'). At least 6 I've come across are unanswered.

I did implement one specific code unsuccessfully (I can't find the snippet for the life of me, but the desired default category used in the example was 'authors'). This is driving me nuts, and I would really appreciate your help. Thanks.

EDIT: I have tried the following code (which I got from here) and got 'uncategorized' to be checked automatically upon save for the post type 'community' but the problem is that it completely overrides other categories you might replace this with. That is to say, if you uncheck 'uncategorized' and choose meaningful categories, upon 'publish' or 'save', all of those selections are erased and it reverts back to community. Need it to just have 'uncategorized' until the user replaces that default category (exactly the way a defaul category works with regular 'post' type).

function add_comm_category_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
$uncategorized= array (1);
wp_set_object_terms( $post_ID, $uncategorized, 'category');
}
}
add_action('save_post', 'add_comm_category_automatically');

I've been searching for a couple hours and consulted multiple posts, but I seem to get this to work.

I have this weird bug where users cannot see their drafts of a custom post type if they don't assign a category to them. So if they just click 'save as draft' and want to go back to it later, it won't be visible to them (I have to go in as an admin and set a category for it to be visible to them). I have no idea why this is occurring but I'm willing to work around it.

In Settings > Writing you can set the default category for a regular post, but there is no such option available for a custom post type. I am fine to set the default type to 'Uncategorized', just like it is set with regular posts. So I'm trying to accomplish this.

Some snippets I've come across like this are aimed for 'default category upon publish' but I need it upon autosave (some users just have access to 'save draft' and 'submit for publishing'). At least 6 I've come across are unanswered.

I did implement one specific code unsuccessfully (I can't find the snippet for the life of me, but the desired default category used in the example was 'authors'). This is driving me nuts, and I would really appreciate your help. Thanks.

EDIT: I have tried the following code (which I got from here) and got 'uncategorized' to be checked automatically upon save for the post type 'community' but the problem is that it completely overrides other categories you might replace this with. That is to say, if you uncheck 'uncategorized' and choose meaningful categories, upon 'publish' or 'save', all of those selections are erased and it reverts back to community. Need it to just have 'uncategorized' until the user replaces that default category (exactly the way a defaul category works with regular 'post' type).

function add_comm_category_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
$uncategorized= array (1);
wp_set_object_terms( $post_ID, $uncategorized, 'category');
}
}
add_action('save_post', 'add_comm_category_automatically');
Share Improve this question edited May 28, 2015 at 15:51 zk87 asked May 28, 2015 at 8:12 zk87zk87 1191 gold badge3 silver badges9 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 6

Use the save_post action hook and in the call back function use wp_set_object_terms( $object_id, $terms, $taxonomy, $append ) function.

For your custom post type the code can be like this

function save_book_meta( $post_id, $post, $update ) {

    $slug = 'book'; //Slug of CPT

    // If this isn't a 'book' post, don't update it.
    if ( $slug != $post->post_type ) {
        return;
    }

    wp_set_object_terms( get_the_ID(), $term_id, $taxonomy );
}

add_action( 'save_post', 'save_book_meta', 10, 3 );

$taxonomy - The context in which to relate the term to the object. This can be category, post_tag, or the name of another taxonomy.

$term_id - term ID of the taxonomy

I do not know your project thoroughly, so you can consider this snippet as a way of doing what you wanted to do.

For more reference visit this two links given below :

https://codex.wordpress/Function_Reference/wp_set_object_terms

https://codex.wordpress/Plugin_API/Action_Reference/save_post

I hope you will find a way out.

I am using pods.io to build my CPTs and custom taxonomies. Had the same problem. With the code from @mishu, I was able to accomplish my goal.

function event_preset_category( $post_id, $post, $update ) {

    $slug = 'termine'; //Slug of CPT

    // If this isn't the right slug, don't update it.
    if ( $slug != $post->post_type ) {
        return;
    }

    // Get the ID of default/ fallback category
    // $default_term = get_term_by('slug', 'your_term_slug', 'your_custom_taxonomy');

    $default_term = get_term_by('slug', 'alle', 'termin_cat');

    wp_set_object_terms( get_the_ID(), $default_term->term_id, 'termin_cat' );
}

add_action( 'save_post', 'event_preset_category', 10, 3 );

If I understand correctly, all you need to do is to add the optional parameter $append as true (the default is false)

wp_set_object_terms( int $object_id, string|int|array $terms, string $taxonomy, bool $append = false )

for example, in the code you posted: wp_set_object_terms( $post_ID, $uncategorized, 'category', true);

that worked for me.

It is possible to do it this way. If other answers didn't work, you can try this:

function auto_category( $post_id, $post, $update ) {
    
    $slug = 'news'; //Slug of CPT
    
    // If this isn't the right slug, don't update it.
    if ( $slug != $post->post_type ) {
        return;
    }
    
    $category_term = get_term_by( 'name', 'news_category_slug', 'category' );
    
    wp_set_object_terms( get_the_ID(), $category_term->term_id, 'category' );
}

add_action( 'save_post', 'auto_category', 10, 3 );
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>