• I have the following code which works perfectly to get me a filter of all the terms (from section) in the custom post type module.

    /* Create filter-through Module's by Section
    ---------------------------------------------------*/
    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;
    }

    Now what my problem is, is getting this to do the same as above, but to filter the parent pages of module. Heres the code i have so far, which gets me the list of all the parent pages in the post type module, the part i cant get is once you click the filter button, to have everything filter properly. I want the outcome to be the parent page and all its children will be shown.

    /* Create filter-through Module's by Parent Module
    ---------------------------------------------------*/
    add_action('restrict_manage_posts', 'restrict_manage_posts_module');
    function restrict_manage_posts_module()
    {
        global $post_type;
        if ( is_object_in_taxonomy( $post_type, 'section' ) )
        {
            $dropdown_options = array(
                'show_option_none' => __( 'View all modules' ),
                'depth' => 1,
                'hierarchical' => 1,
                'post_type' => 'module',
                'sort_column' => 'name',
                'selected' => $page
            );
    
            add_filter('wp_dropdown_pages', 'wp_dropdown_module_filter', 10);
            wp_dropdown_pages( $dropdown_options );
            remove_filter('wp_dropdown_pages', 'wp_dropdown_module_filter', 10);
        }
    }
    
    function wp_dropdown_module_filter($select)
    {
        $pages  = get_pages(array());
        foreach( $pages as $page )
        {
            $select = str_replace('value="'.$page->ID.'"', 'value="'.$page->post_name.'"', $select);
            if (isset($_GET['module']) && $page->post_name == $_GET['module']){
                $select = str_replace('value="'.$page->post_name.'"', 'value="'.$page->post_name.'" selected', $select);
            }
        }
        return $select;
    }
  • The topic ‘Admin custom page filter’ is closed to new replies.