WordPress.org

Forums

Custom Taxonomy filters (3 posts)

  1. webrob
    Member
    Posted 1 year ago #

    I am needing to create a custom taxonomy structure for my posts. I have read the WP Codex and am confident I can pull that off. My custom taxonomy will be hierarchical.

    What I need to do is to create a filter based on this new taxonomy. I need to do this with drop down boxes.

    Here is an example of my custom taxonomy will be:

    - Artlcles
    --- Article 1
    --- Article 2
    --- Article 3
    - Amendments
    --- Amendment 1
    --- Amendment 2
    --- Amendment 3

    What I need is to have 2 drop down boxes. The first one will have all of the top level categories (Articles, Amendments, etc...) and then when one of those is selected it will populate/make appear a second drop down with all of the subcategories.

    How can I achieve this?

  2. webrob
    Member
    Posted 1 year ago #

    I have put the following code together and maybe someone can help me figure out the last piece of the puzzle. You can also see what is happening at the URL http://www.constitutingamerica.org/dev2/

    <?php $args = array(
    	'show_option_all'    => '',
    	'show_option_none'   => '',
    	'orderby'            => 'ID',
    	'order'              => 'ASC',
    	'show_count'         => 1,
    	'hide_empty'         => 1,
    	'child_of'           => 0,
    	'parent'			 => 0,
    	'exclude'            => '',
    	'echo'               => 1,
    	'selected'           => 0,
    	'hierarchical'       => 0,
    	'name'               => 'cat',
    	'id'                 => '',
    	'class'              => 'postform',
    	'depth'              => 1,
    	'tab_index'          => 0,
    	'taxonomy'           => 'classification',
    	'hide_if_empty'      => false,
        'walker'             => ''
    );
    
    $tax_menu_items = get_categories( $args ); ?>
    
    <form name="class-filter" method="get" >
    <select name="class-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
    <option value="">Select</option>
    <? foreach ( $tax_menu_items as $tax_menu_item ): ?>
        <option value="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>"><?php echo $tax_menu_item->name; ?></option>
    <?php endforeach; ?>
    </select>
    <?php
    
    $sub_tax_id = $wp_query->get_queried_object_id();
    if($wp_query->get_queried_object_id()) {
    $args2 = array(
    	'show_option_all'    => '',
    	'show_option_none'   => '',
    	'orderby'            => 'ID',
    	'order'              => 'ASC',
    	'show_count'         => 1,
    	'hide_empty'         => 1,
    	'child_of'           => 0,
    	'parent'			 => $sub_tax_id,
    	'exclude'            => '',
    	'echo'               => 1,
    	'selected'           => 0,
    	'hierarchical'       => 0,
    	'name'               => 'cat2',
    	'id'                 => '',
    	'class'              => 'postform',
    	'depth'              => 1,
    	'tab_index'          => 0,
    	'taxonomy'           => 'classification',
    	'hide_if_empty'      => true,
        'walker'             => ''
    );
    
    $tax_menu_items2 = get_categories( $args2 ); ?>
    
    <select name="class-dropdown2" onchange="document.location.href=this.options[this.selectedIndex].value;" >
    <option value="">Select</option>
    <? foreach ( $tax_menu_items2 as $tax_menu_item2 ): ?>
        <option value="<?php echo get_term_link($tax_menu_item2,$tax_menu_item2->taxonomy); ?>"><?php echo $tax_menu_item2->name; ?></option>
    <?php endforeach; ?>
    </select>
    <? } ?>
    </form>

    The only thing I need to figure out is how to keep the second drop down menu populated once it has been selected. Any ideas?

  3. webrob
    Member
    Posted 1 year ago #

    I was looking through my code more and testing it out on the link I posted http://www.constitutingamerica.org/dev2/. The first drop down box does not change which is what I want. The second box just clears its results. What I think is happening is that the second box is dependent on the $wp_query->get_queried_object_id(); variable I am grabbing to populate the second drop down. So when the second box is changed it is looking for children of the last query. To keep the results in the second box I need to grab the ID of the custom taxonomy that is selected in the first box. I have tried a bunch of different variables from the first half of the code that creates the first box but nothing seems to stick.

    Any ideas on what I can grab from that first code to keep that second box populated with all of the child results?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags