Filters post by subcategory slug using buttons

Viewed 36

I load in subpage post with one category now i try filter this post by him subcategory and get link to page some like mypage.com/subpage/?slug=fish after click button and see only fish post.

I should use $_GET?

Anyone can help?

There is my code who load all my post from category animals

  <?php

        $args = array(
            'cat' => 50,
            'posts_per_page' => '999',
            'order' => 'ASC',
            'orderby' => 'menu_order',
        );
        $the_query = new WP_Query($args);
        // The Loop
        if ($the_query->have_posts()) {
            ?>

            <?php

            while ($the_query->have_posts()) {
                $the_query->the_post();

                get_template_part( 'templates/content/content', 'animals' ); 

            }
            /* Restore original Post Data */
            wp_reset_postdata();
        } else {
            // no posts found
            echo '';
        }
    ?>
    
1 Answers

You can used like this :

if(isset($_GET['slug']))
{
$args = array(
    'post_type' => 'post_type',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $_GET['slug']
        )
     )
);
}
Related