最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

query - Filter custom post types in archive

programmeradmin6浏览0评论

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!

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!

Share Improve this question asked Oct 10, 2020 at 0:54 TantaalTantaal 233 bronze badges 6
  • 1 When you submit the form, what's the URL? Have you considered linking to the terms archive instead? – Tom J Nowell Commented Oct 10, 2020 at 2:22
  • The URL is: localhost/project/pumps/?series=7 It looks okay, but I'm getting the 404 And I didn't understand your second question – Tantaal Commented Oct 10, 2020 at 17:02
  • 1 and you're absolutely sure that it takes a term ID? I tried reproducing this with tags and could not get it to work, but ?tag=react worked perfectly for me. – Tom J Nowell Commented Oct 10, 2020 at 17:21
  • yes, I'm sure. I checked the custom taxonomx ID in the backend – Tantaal Commented Oct 10, 2020 at 17:42
  • 1 I didn't mean are you sure the term ID is right, but I meant are you sure you need to use a term ID? Not a term slug? ?tag=react worked for me, but using the react terms ID did not. I'm assuming though that there is no query_posts call on this page? – Tom J Nowell Commented Oct 10, 2020 at 18:40
 |  Show 1 more comment

1 Answer 1

Reset to default 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/cpt/?mytax=1 404
  • example/cpt/?mytax=helloworld a cpt archive filtered by the helloworld term
发布评论

评论列表(0)

  1. 暂无评论