我按年份将分类法(更多分类法)分类为自定义后置类型。我想要的是我的自定义帖子类型,当我要添加新帖子时,自动将当前年份选择为默认分类术语。
因此,在伪代码中,我想为术语名称为date(“ Y”)的自定义帖子类型设置默认分类术语。
有什么想法可以做到这一点,和/或如何设置默认分类法术语?
您可以使用以下内容:
function prefix_post_year( $post_id ) {
$current_post = get_post( $post_id );
// This makes sure the taxonomy is only set when a new post is created
if ( $current_post->post_date == $current_post->post_modified ) {
wp_set_object_terms( $post_id, date( 'Y' ), {taxonomy}, true );
}
}
add_action( 'save_post_{post_type}', 'prefix_post_year' );
笔记:
{post_type}
为您的帖子类型名称。因此,如果您的帖子类型为“项目”,则操作应为save_post_project
{taxonomy}
您的分类名称wp_set_object_terms( $post_id, date( 'Y' ), 'project-year', true );
date( 'Y' )
为'project-posted-in-' . date( 'Y' )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句