如何将默认分类分配给‘SAVE_POST’上的页面?

时间:2012-05-13 作者:JSS

我尝试将自定义分类分配给page 通过“发布”按钮新增时。

这是功能:

function set_default_object_terms( $id, $post ) {
if ( \'publish\' === $post->post_status ) {

    log_me (\'From inside function: \'.__FUNCTION__.\', while I pressed the "publish" button. Post-ID: \'.$id);

    $taxonomy_ar = get_terms( \'property-features\', \'\' );

    foreach ($taxonomy_ar as $taxonomy_term) {          
        log_me (\'Inside the function: \'.__FUNCTION__.\' and inside the "foreach"-loop for the ID: \'.$id.\' and Term: \'. $taxonomy_term->name . \' and Post-ID :\'. $post->ID);
        wp_set_object_terms( $post->ID, $taxonomy_ar, $taxonomy_term->name, true );  
    }
}}
这是钩子:

add_action( \'save_post\', \'set_default_object_terms\', 100, 2 );
在日志文件中,我添加了日志文件,以确定是否找到了所有值,并找到了所有自定义分类:

[13-May-12 16:28] be in function while "publish" is pressed with this id: **64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Kitchen** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Stove** and Post-ID :**64**
[13-May-12 16:28] In the "foreach" with id: 64 and Term: **Pets ok** and Post-ID :**64**
但它没有分配它。有人知道把戏在哪里吗?

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您正在使用wp_set_object_terms 错误,第二个参数应该是术语slug或id,第三个参数应该是分类名称,因此请尝试:

function set_default_object_terms( $id, $post ) {
    if ( \'publish\' === $post->post_status ) {

        log_me (\'be in function while "publish" i pressed with this id: \'.$id);

        $taxonomy_ar = get_terms( \'property-features\' );
        if (count($taxonomy_ar) > 0){
            foreach ($taxonomy_ar as $taxonomy_term) {          
                //create an arry with all term ids
                log_me (\'In the "foreach" with id: \'.$id.\' and Term: \'. $taxonomy_term->name . \' and Post-ID :\'. $post->ID);
                $term_ids[] = $taxonomy_term->ID;
            }
            wp_set_object_terms($post->ID,$term_ids,\'property-features\',true);
        }
    }
}
并确保使用register_taxonomy_for_object_type 例如:

register_taxonomy_for_object_type(\'property-features\',\'page\');

SO网友:golden_grahams

我有一个代码片段可以在加载编辑页面时执行此操作,而不是在保存时执行。(这样用户可以在点击publish之前查看所选内容,在post\\u save上分配默认术语也就不足为奇了)。

我已经有了一个将复选框更改为单选按钮的功能,但它似乎也是添加的适当位置checked 至其中一个输入。

用于添加新项,用于编辑现有项。

// Press type taxonomy
// - Replace checkboxes for radio buttons
// - Select "Press release" by default
add_filter(\'wp_terms_checklist_args\', \'press_type_radio_checklist\');

function press_type_radio_checklist($args)
{

    $radio_taxonomies = [
        \'press_type\',
    ];

    if (!empty($args[\'taxonomy\']) && in_array($args[\'taxonomy\'], $radio_taxonomies)) {
        if (empty($args[\'walker\']) || is_a($args[\'walker\'], \'Walker\')) {

            // Radio button taxonomy walker
            if (!class_exists(\'Walker_Radio_Checklist\')) {
                class Walker_Radio_Checklist extends Walker_Category_Checklist
                {

                    function walk($elements, $max_depth, $args = [])
                    {

                        $output = parent::walk($elements, $max_depth, $args);
                        $output = str_replace([
                            \'type="checkbox"\',
                            "type=\'checkbox\'",
                        ], [
                            \'type="radio"\',
                            "type=\'radio\'",
                        ], $output);

                        // If there aren\'t any selected cats, select the default
                        if (empty($args[\'selected_cats\'])) {
                            $default_term = get_term_by(\'name\', \'Press release\', \'press_type\');

                            if (!empty($default_term) && $default_term instanceof WP_Term) {
                                $default_term_id = $default_term->term_id;

                                $output = str_replace([
                                    \'input value="\' . $default_term_id . \'"\',
                                    "input value=\'$default_term_id\'",
                                ], [
                                    \'input value="\' . $default_term_id . \'" checked\',
                                    "input value=\'$default_term_id\' checked",
                                ], $output);
                            }
                        }

                        return $output;
                    }
                }
            }

            $args[\'walker\'] = new Walker_Radio_Checklist;
        }
    }

    return $args;
}

结束

相关推荐