0

I've created a custom taxonomy 'animal-species' that I want to filter out my 'animals' custom post type. The idea is that a user can go to the main animals page and filter them out by cats, dogs etc. I've created a taxonomy-animals-animal-species.php page, but the domain.com/animals/?animal-species=cat (or dog or whatever) still lists all of the animals, not just the ones that match the taxonomy. I've tried re-naming the taxonomy page to every variation that I can think of with no success. Does anyone have any ideas on how to fix this?

TLDR: domain.com/animals/?animal-species=cat and domain.com/animals/ give the same results

Code for both the animal-listing.php page and taxonomy-animals-animal-species.php pages

<?php
            $page_link = get_permalink();
            $animal_species = get_terms( 'animal-species' );

            $args = array();
            $args['post_type'] = 'animal';
            if ( isset( $_GET['animal-species'] ) ) {
                $args['animal-species'] = $_GET['animal-species'];
                $page_link = add_query_arg( 'animal-species', $_GET['animal-species'], $page_link );
            }

            if ( is_array( $animal_species ) ) {
                echo '<ul class="species_list">';
                echo '<li><a href="' . htmlentities( remove_query_arg( 'animal-species', $page_link ) ) . '">Show all animals</a></li>';
                foreach ( $animal_species as $animal_species ) {
                    echo '<li><a href="' . htmlentities( add_query_arg( 'animal-species', $animal_species->slug, $page_link ) ) . '">' . $animal_species->name . 's</a></li>';
                }
                echo '</ul>';
            }
        ?>


            <div class="listing">
                    <?php 
                        $animals_per_page = 10;

                        if( function_exists( 'ot_get_option' ) )
                        {                               
                            $animals_per_page = intval(ot_get_option('animals_per_page'));
                        }

                        $args = array(
                                    'post_type' => 'animal',
                                    'posts_per_page' => $animals_per_page,
                                    'paged' => $paged
                                );

                        $query = new WP_Query( $args );

                        $i = 0;                     
                        if ( $query->have_posts() ) 
                        {

                            while ( $query->have_posts() ) : 

                                $query->the_post(); 
                                $i++;

                                    ?>                          
                                    <div class="animal-item <?php if ($i % 2 == 0) { echo "even"; } ?>" >
                                        <?php get_template_part( 'inc/display-animal' ); ?>                        
                                    </div>                           
                                    <?php

                            endwhile;
                        }
                ?>                          
            </div>
| improve this question | | | | |
  • Without the code for the page this is going to be difficult to answer. – s_ha_dum Jul 15 '13 at 2:23
  • The main animal-listing.php page, taxonomy-animals-animal-species.php is identical – ColoursB Jul 15 '13 at 2:37
1

You're not querying for a specific taxonomy term, you set $args['animal-species'], but then you overwrite the $args var later, before the query. also, that form of taxonomy query is deprecated, see tax_query in Codex.

| improve this answer | | | | |
  • Could you give me a working example? – ColoursB Jul 15 '13 at 3:36
  • a working example of what exactly? tax_query parameters are shown on the page I linked, substitute your taxonomy and term names. – Milo Jul 15 '13 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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