首页 > 解决方案 > WordPress - 使用参数设置自定义帖子类型默认类别(分类术语)

问题描述

我有多种自定义帖子类型,我想设置一个默认类别,可以通过参数从一个函数设置。我最初为一种自定义帖子类型执行此操作并且是硬编码的,但我认为我会这样做,所以我们只需要输出带有参数的函数,以防我们决定添加更多自定义帖子类型。

问题在于函数内部 WordPress 似乎没有链接它自己的钩子和函数,并且在尝试通过 wp_set_object_terms() 函数设置它时返回无效的分类错误。当我完成此操作时,WordPress 方式将使您为每个帖子类型和您想要的默认术语创建一个单独的函数,我在下面放了。

function set_default_object_terms( $post_id, $post ) {
        if ( 'publish' === $post->post_status && $post->post_type === 'comic' ) {
            $defaults = array(
                'story' => array( 'draft' )https://silentcomics.com
                );
            $taxonomies = get_object_taxonomies( $post->post_type );
            foreach ( (array) $taxonomies as $taxonomy ) {
                $terms = wp_get_post_terms( $post_id, $taxonomy );
                if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                    wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
                }
            }
        }
    }
    add_action( 'save_post', 'set_default_object_terms', 0, 2 );

我想要实现的是如下所示,但 WordPress 的全局变量都没有工作,也没有任何 wordpress 函数,如 WordPress 给出的示例中使用的 get_object_taxonomies() 或 wp_set_object_terms()。

下面是我试图实现的简化版本,我试图通过的参数是 cpt slug、分类 slug 和我想要回退到的类别的 id。我也尝试过将变量 $cat_id 作为数组。

function set_default_term( $cpt_slug, $taxonomy_slug, $term_id ) {
    if ( isset( $_GET['post'] ) ) {
        $post_id   = intval( $_GET['post'] );
        $post_type = get_post_type( $post_id );
        $status    = get_post_status( $post_id );
        $cat_id    = array( $term_id );
        $cat_id    = array_map( 'intval', $cat_id );
        $cat_id    = array_unique( $cat_id );
        if ( $status === 'publish' && $post_type === $cpt_slug ) {
            wp_set_object_terms( $post_id, $cat_id, $taxonomy_slug, true );
        }
    }
}

add_action( 'save_post', 'set_default_term', 20, 3 );

set_default_term('deployment-guides', 'deployment-guide-category', 98);

标签: wordpresscustom-post-typecustom-taxonomytaxonomy-terms

解决方案


嗨,布拉德,我检查了您的代码,只是将帖子类型post和分类法替换为post_tag只是为了测试一般原则,它运行良好。请参阅下面我用来检查的一些修改过的代码。

function my_custom_set_default_terms_for_posts( $post_ID, $post ) {

    if ( 'publish' === $post->post_status && 'post' === $post->post_type ) {
        $defaults   = array(
            'post_tag' => array( 'example-default-tag-slug' ),
        );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_ID, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {

                $terms_to_attach = $defaults[ $taxonomy ];

                if ( count( $terms_to_attach ) ) {
                    wp_set_object_terms( $post_ID, $terms_to_attach, $taxonomy );
                }
            }
        }
    }
}

add_action( 'save_post', 'my_custom_set_default_terms_for_posts', 0, 2 );

并且更多地考虑returning invalid taxonomy errors when trying to set it via the wp_set_object_terms()它可能发生的实际问题并不是因为您的save_post处理程序做错了 - 这是一个很好的正确代码。

您需要检查如何初始化分类法和自定义帖子类型 (CPT),理想情况下,您应该在挂钩上执行此操作,以便在此挂钩或其他依赖于分类法\cpt 存在的代码运行init之前初始化您的 CPT 和自定义分类法。save_post

此外,当您初始化 CPT 并绑定到它的分类时,您需要先初始化 CPT register_post_type(),然后初始化绑定到它的分类register_taxonomy(),因为通常当您注册分类时,它期望使用一个 Post 类型数组,并且它们需要在注册分类学之前进行注册。


推荐阅读