如何将 Wordpress CPT 默认标题设置为 Post Meta

How to set Wordpress CPT Default Title to Post Meta

我正在尝试将自定义 post 类型的默认 post 标题设置为 post 类别,然后是 space 和发布日期。但是我收到一个错误。我已经尝试了很多不同的变体。

function add_default_podcast_title( $data, $postarr ) {
if($data['post_type'] == 'podcasts') {
    if(empty($data['post_title'])){
        $ashow = get_the_category();
        $publishdate = the_date('M j');
        $data['post_title'] = $ashow.' '.$publishdate;
    }
}
return $data;
}
add_filter('wp_insert_post_data', 'add_default_podcast_title', 10, 2 );

get_the_category() returns 一个数组,但在您的回调函数中,您将该数组用作字符串。这就是为什么会出现该错误。这是你的代码的简化和修复版本,我还没有测试过,但它应该可以工作。

function add_default_podcast_title( $data, $postarr ) {
    if ( 'podcasts' === $data['post_type'] && empty( $data['post_title'] ) ) {
        $ashow = 'prefix';
        $categories = get_the_category();
        $publishdate = the_date( 'M j' );
        if ( ! empty( $categories ) ) {
            $first_category = current( $categories );
            $ashow = $first_category->name;
        }
        $data['post_title'] = $ashow . ' ' . $publishdate;
    }
    return $data;
}
add_filter( 'wp_insert_post_data', 'add_default_podcast_title', 10, 2 );