Taxonomy filter all children

时间:2011-06-25 作者:cnotethegr8

我有一个自定义分类过滤器,它将过滤选定分类中的所有页面。我希望代码能够选择该分类法中的页面,以及千页的子页面。

这是密码。

add_action(\'restrict_manage_posts\', \'restrict_manage_posts_section\');
function restrict_manage_posts_section()
{
    global $post_type;
    if ( is_object_in_taxonomy( $post_type, \'section\' ) )
    {
        $dropdown_options = array(
            \'show_option_all\' => __( \'View all sections\' ),
            \'hide_empty\' => 0,
            \'hierarchical\' => 1,
            \'name\' => \'section\',
            \'show_count\' => 0,
            \'taxonomy\' => \'section\',
            \'orderby\' => \'name\',
            \'selected\' => $cat
        );

        add_filter(\'wp_dropdown_cats\', \'wp_dropdown_section_filter\', 10);
        wp_dropdown_categories( $dropdown_options );
        remove_filter(\'wp_dropdown_cats\', \'wp_dropdown_section_filter\', 10);
    }
}

function wp_dropdown_section_filter($select)
{
    $terms  = get_terms(\'section\', array(\'hide_empty\' => false));   
    foreach( $terms as $term )
    {
        $select = str_replace(\'value="\'.$term->term_id.\'"\', \'value="\'.$term->slug.\'"\', $select);
        if (isset($_GET[\'section\']) && $term->slug == $_GET[\'section\']){
            $select = str_replace(\'value="\'.$term->slug.\'"\', \'value="\'.$term->slug.\'" selected\', $select);
        }
    }   
    return $select;
}
编辑

这是我的自定义帖子类型和分类功能

/* Register Custom Post Type and Taxonomy
---------------------------------------------------*/
add_action(\'init\', \'register_module_type\');
function register_module_type() {
    $labels = array(
        \'name\' => _x(\'Modules\', \'post type general name\'),
        \'singular_name\' => _x(\'Modules\', \'post type singular name\'),
        \'add_new\' => _x(\'Add Module\', \'module item\'),
        \'add_new_item\' => __(\'Add Module\'),
        \'edit_item\' => __(\'Edit Module\'),
        \'new_item\' => __(\'New Module\'),
        \'view_item\' => __(\'View Module\'),
        \'search_items\' => __(\'Search Module\'),
        \'not_found\' =>  __(\'Nothing found\'),
        \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
        \'parent_item_colon\' => \'\'
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'module\', \'with_front\' => false ),
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'has_archive\' => true,
        \'can_export\' => true,
        \'menu_position\' => null,
        \'supports\' => array(\'title\',\'editor\',\'thumbnail\',/*\'excerpt\',*/\'revisions\',\'custom-fields\',\'post-formats\'/*,\'page-attributes\'*/)
        #\'taxonomies\' => array(\'category\', \'post_tag\')
    ); 

    register_post_type( \'module\' , $args );
    #register_taxonomy_for_object_type(\'category\', \'testimonial\');
    #register_taxonomy_for_object_type(\'post_tag\', \'testimonial\');

    $labels = array(
        \'name\' => _x( \'Sections\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Section\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Search Sections\' ),
        \'all_items\' => __( \'All Sections\' ),
        \'parent_item\' => __( \'Parent Section\' ),
        \'parent_item_colon\' => __( \'Parent Section:\' ),
        \'edit_item\' => __( \'Edit Section\' ),
        \'update_item\' => __( \'Update Section\' ),
        \'add_new_item\' => __( \'Add New Section\' ),
        \'new_item_name\' => __( \'New Section Name\' ),
    );  

    register_taxonomy( \'section\', array( \'module\' ), array(
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'section\' ),
    ));

    #add_theme_support( \'post-formats\', array( \'chat\',\'aside\',\'gallery\',\'link\',\'image\',\'quote\',\'status\',\'video\' ));
    flush_rewrite_rules( false );
}

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

这将钩住任何帖子的更新操作。它将一组给定分类法的所有术语从父项复制到其子项。

/**
 * Update all children of a post with the same terms as itself.
 * 
 * @param int $post_ID
 * @param object $post
 */
function __update_children_with_terms( $post_ID, $post )
{
    global $wpdb;

    // array of taxonomies to be copied to children, if the post type supports it
    $taxonomies = array( \'section\' );

    if ( ! is_post_type_hierarchical( $post->post_type ) )
        return; // bail

    // all child IDs for current post
    $children = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_parent = " . ( int ) $post_ID );

    // loop over each taxonomy for the current post type
    foreach ( get_object_taxonomies( $post->post_type ) as $taxonomy ) {

        if ( ! in_array( $taxonomy, $taxonomies ) )
            continue; // bail, term copy not supported for this tax

        if ( ! $terms = wp_get_object_terms( $post_ID, $taxonomy, array( \'fields\' => \'ids\' ) ) )
            continue; // bail, no terms for this tax

        // essential, otherwise wp_set_object_terms treats them as strings and creates new terms!
        $terms = wp_parse_id_list( $terms );

        // loop over children and append the terms
        foreach ( $children as $child ) {
            wp_set_object_terms( $child, $terms, $taxonomy, true );

            // this will rescursively iterate down the tree but at a cost!!
            // remove it if you only need parent => direct child copying
            wp_update_post( array( \'ID\' => ( int ) $child ) );
        }

    }
}
add_action( \'wp_insert_post\', \'__update_children_with_terms\', 10, 2 );
注意内部的最后一行foreach 循环-如果您只有Top Level Parent => Children, 而不是Parent => Child => Grandchild, 我强烈建议删除以下行;

wp_update_post( array( \'ID\' => ( int ) $child ) );
这是一种递归情况,将在子级上循环并运行相同的进程,继续迭代,直到处理完整个树。

结束