Filtering WP_query through SEO friendly URL.
-
I have a custom post type
register_post_type( 'vegetables', $args );
each of my custom posts with post type ‘vegetables’ have custom meta tags like
‘custom_vegetable_color’ or ‘custom_vegetable_size’ etc.I list my custom posts on custom page template with simple loop for putting them into table
<?php $args = array( 'post_type' => 'vegetables' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?>
this page takes url domain.com/vegetable-list/
What i want to do is, being able to link to list containing only posts with ‘custom_vegetable_color’ == ‘green’;
I know how to do that in my WP_Query, by simple adding argument$args = array( 'post_type' => 'vegetables', 'meta_key' => 'custom_vegetable_color', 'meta_value' => 'green' );
But that would mean that i have to create custom page for every possible meta value, which is very unpracticle and completely not dynamical. I want to be able to link to domain.com/vegetable-list/green/ which would then list only vegitable with meta_value ‘green’. (in case that it is not possible with meta values, i can do this with taxonomies, i had no luck trying it with them)
I know there is possibility how to pass argument thru URL
if( !empty($_GET['color']) ) $color=$_GET['color']; $args = array( 'meta_value' => $color ); $loop = new WP_Query( $args );
But that creates not SEO friendly url like domain.com/vegetable-list/?color=green, which i do not want.
Also i see problem there with ‘meta_key’, which wouldn’t be always the same.I’m really lost as to where to start, i can see possibility of custom .htaccess redirect urls (domain.com/vegetable-list/green/ = domain.com/vegetable-list/?color=green), which is the same effort as making each page individualy.
Is there “easy” but mainly dynamic way to solve this? Is working with taxonomies the right way? How do i link to SEO friendly URL without creating individual pages? Thanks alot for your time.
- The topic ‘Filtering WP_query through SEO friendly URL.’ is closed to new replies.