Filter custom post types in archive

I want to filter my custom posts (pump) with a custom filter form on it’s archive page (archive-pump.php).

So, I wrote the form markup:

<form method="GET">
    <label>Series</label>
    <?php
        if( $terms = get_terms( array( 'taxonomy' => 'series', 'orderby' => 'name' ) ) ) :
            echo '<select name="series">';
            foreach ( $terms as $term ) :
                echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
            endforeach;
            echo '</select>';
        endif;
    ?>
    <button type="submit">Apply filter</button>
</form>

And I have this to output my custom posts:

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
    <?php the_title( '', '', true ); ?>
<?php endwhile; endif; ?>

When I open my page (localhost/project/pumps) it looks fine.
But when I submit my form I’m getting an 404 page.

I maybe need an seperate query to fetch all the $_GET data. But I’m not getting to the step because of the 404 error.

What am I doing wrong? Thank you!

1 Answer
1

The problem is that you’re using term IDs in your URL, but that is incorrect.

Use the term slug instead.

For example, lets say we have a mytax term named helloworld with the term ID 1:

  • example.com/cpt/?mytax=1 404
  • example.com/cpt/?mytax=helloworld a cpt archive filtered by the helloworld term

Leave a Comment