Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom taxonomy filter that will filter all the pages in the selected taxonomy. I would like the code to select the pages in that taxonomy, and the children of thous pages.

Heres the code.

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;
}

EDIT

Here is my Custom Post Type and Taxonomy Function

/* 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 );
}
share|improve this question
    
What's not working? Describe the problem as much as you can. –  TheDeadMedic Jun 26 '11 at 11:13
    
Currently this function does work correctly. But i want it to work a little differently. Lets say i have a Parent page and that page has 5 children pages, but only the Parent page is assigned a taxonomy. What i want to happen is when i select that taxonomy from the drop-down list (provided by this function) i want the Parent and all of its children to be shown. Currently only the Parent is shown, since its the only one specified in the selected taxonomy. –  cnotethegr8 Jun 26 '11 at 11:18
    
Unfortunately, that's the normal behaviour of WP_Query - I think there'd be quite a bit of work involved in changing this. You'd need to hook into the row display methods of the WP_Posts_List_Table and query children of the current item, then recursively add them to the table & check for grandchildren. –  TheDeadMedic Jun 26 '11 at 12:29
    
hmm... I dont know if i could write that myself. My PHP is not very good. Do you think it would make more sense to have a function that looks at the Parent Taxonomy and than assigns it to its Children? Could this be easily done? –  cnotethegr8 Jun 26 '11 at 13:11
    
If that's the behaviour you want, then it would definitely be a more logical approach. –  TheDeadMedic Jun 26 '11 at 16:02
add comment

1 Answer

up vote 2 down vote accepted

This hooks into the update action for any post. It'll copy all terms for a given set of taxonomies from a parent to it's children.

/**
 * 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 );

Note the last line of the inner foreach loop - if you only have Top Level Parent => Children, and not Parent => Child => Grandchild, I highly recommend removing the following line;

wp_update_post( array( 'ID' => ( int ) $child ) );

It's a recursive situation, that will loop over children and run the same process, continuing to iterate until the whole tree has been processed.

share|improve this answer
    
This looks great! Thanks. For some reason im having a little trouble with it though. When you say this will work with all posts, you ment custom post types, correct? This is for a custom post type called 'Module'. I tried submitting a few of the custom posts several times through the quick edit and the normal way. Iv'e had no luck... –  cnotethegr8 Jun 26 '11 at 16:33
    
Can you post your register_post_type and register_taxonomy code? –  TheDeadMedic Jun 26 '11 at 16:44
    
Sure, I'll update it to my main post, up top. –  cnotethegr8 Jun 26 '11 at 17:04
    
I've just copied your code & mine to a local install and it runs fine! I added a section Foobar, then created a module Parent. Then I added module called Child and set it's post parent to Parent. Then I (quick) edited Parent, checked Foobar, and clicked 'Save'. Reloaded the page, clicked quick edit for Child, and Foobar was now checked! –  TheDeadMedic Jun 26 '11 at 17:21
    
hmm very strange... I just uploaded to to my live site and it worked. But it dosnt work on my local host. Im using MAMP (for mac) Do you have any idea why this would be? –  cnotethegr8 Jun 26 '11 at 17:43
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.