Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Barry Denson
Courses Plus Student 13,188 PointsFiltering custom post types by category
I am building a wordpress site for a telecoms company which has a products page. The page will have 2 categories of products, headsets and telephones.
The page will have 2 icons at the top which will link to the 2 categories. When the headset icon is clicked a slider will show the thumbnail images of all the headsets. Clicking on a product thumbnail will take you to the posts page of that product, and if you click the telephones icon it will show the telephone products and so on.
I have already created custom post types for the products, and within the CPT I have 2 categories.
How do I filter the posts by category? Also, how can I update the posts query when the icon is clicked without reloading the page? Will I need to use jQuery?
Hope I have explained that clearly!
Any questions just ask!
Thanks
Baz
2 Answers
Andrew Shook
31,709 PointsThey only why to update the page without reloading is to use AJAX. You can pass data back to the server by using a GET request. Try adding this to the telephone and head set buttons
<a href="<?php the_permalink(): // you want the permalink for the page that shows all the products. Link example.com/products?>?product=telephone">Telephones</a>
<a href="<?php the_permalink(): ?>?product=headsets">Headsets</a>
So when a user clicks on one of these buttons the server will be passed info via the $_GET variable. Remember to set "?product=" equal to the taxonomy slugs for telephones and head set respectively.
Then you will need to create a custom db query based on the $_GET variable. Try this custom WP_query :
<?php
$args = array(
'post_type' => 'POST_TYPE_SLUG',
);
if( is_set($_GET["product"] ){
$args['tax_query'] = array(
array(
'taxonomy' => 'CUSTOM_TAXONOMY_SLUG',
'field' => 'slug',
'terms' => $_GET['product']
)
);
}
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : //loop for custom query ?>
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h1>DISPLAY CUSTOM LOOP DATA HERE</h1>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else: ?>
<?php endif; ?>
This will check to see if a user has clicked a button, and If they have it will add a taxonomy requirement to the products query. If a user hasn't clicked a button is should return all products.
NOTE: I did not sanitize the $_GET variable I passed to the $args array. You probably should do that to prevent an injection attack. Just in case.
Hugo Paz
15,622 PointsFor filtering post by category, theres a good piece of text here https://wordpress.org/support/topic/show-categories-filter-on-custom-post-type-list