通过邮政新邮政格式设置WordPress邮政格式



我想在创建新帖子时将帖子设置为帖子类型"状态"。我不想选中该框,因为那是一次单击。我希望我的书签有一种语法,例如:wp-admin/post-new.php?我不想更改我的默认格式,我想在新的帖子创建中进行。

有没有办法做这项工作?

您可以使用save_post操作来执行此处引用的

function update_post_status( $post_id ) {
  set_post_type( $post_id, 'status' );
}
add_action( 'save_post', 'update_post_status' );

编辑:您可以相应地执行检查以验证并检查您是否正在从事实际要更新的帖子。Codex链接示例中的更多验证技术。

我想您想要什么是为自定义帖子类型具有特定的帖子格式。以下是可能有帮助的代码。我已经使用get_the_terms钩获取所有条款,并且对于特定的帖子类型,仅返回音频帖子格式。您可以使用希望拥有的任何帖子格式更改音频。

请用您使用的帖子类型替换your_post_type_name

add_filter( 'get_the_terms', 'my_post_format_terms', 10, 3 );
function my_post_format_terms( $terms, $post_id, $tax ) {
    if ( 'post_format' === $tax && empty( $terms ) && 'your_post_type_name' === get_post_type( $post_id ) ) {
        $audio = get_term_by( 'slug', 'post-format-audio', $tax );
        $terms = array( $audio );
    }
    return $terms;
}

最新更新