How can I rewrite URLs to pass taxonomy and post type values to the query?

So I have a site with about 6 custom post types.Three of these post types are attached to a single custom taxonomy.

I would like to be able to list_cats in the sidebar and spit out a url like ../%post_type%/%taxonomy%/ and have this take you to the taxonomy-%taxonomy%.php template and return only results of the %post_type%.

I can create a conditional that will read the current %post_type% and style correctly once in the taxonomy-%taxonomy%.php file. I need some direction on the most effective method to modify the URL to pass the %post_type% to the query.

Thanks in advance. I have searched for a couple days with no clear answer.

So here are my feeble attempts at a solution yet they are one big FAIL
This is based off a discussion found at rlmseo.com ant the answer here.

Trying to add the post type to a variable to use in the Query:

practice-areas is a taxonomy. Trying to add a variable that I can use to filter the taxonomy by post type in taxonomy-practice-areas.php

function add_query_vars($aVars) {
    $aVars[] = "cust_pt_var";    // represents the name of the custom post type as shown in the URL
    return $aVars;
}

// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');      

function add_rewrite_rules($aRules) {
    $aNewRules = array('practice-areas/([^/]+)/pt/([^/]+)/?$' => 'index.php?practice-areas=$matches[1]&cust_pt_var=$matches[2]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules')

Trying to pass query variables in the rewrite

Trying to add the post types (portfolio & clients) as well as the practice-areas taxonomy term to the query at rewrite.

function add_rewrite_rules($aRules) {
    $aNewRules = array('portfolio/practice-areas/([^/]+)/?$' => 'index.php?post_type=portfolio&practice-areas=$matches[1]');
    $aNewRules = array('clients/practice-areas/([^/]+)/?$' => 'index.php?post_type=clients&practice-areas=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules')

1
1

Ok so I chucked the rewrite all together and went with a $_POST solution. It works really well and no slop in my URL. Here practice-areas is the taxonomy and the link pulls the taxonomy-practice-areas.php template file. On the destination (or here in the template file) I can modify the $query_string to further refine the search to a particular post-type. Yeeeee-ha!!

Put in Destination Page:

    <?php
    $myvar = $_POST['formVar'];
    query_posts($query_string . '&post_type=".$myvar );
    ?>

Put in Originating Page:

<!-- The Form: Put this form somewhere on the page: -->

    <form method=post name="post_form" action="">
    <input type="hidden" name="formVar" value="">
    </form>


<!-- The URL: Here I am assigning the Destination URL to a variable.  This can be whatever you want the destination page to be. -->

    <?php                       
    $dest_url = get_bloginfo("url').'/practice-areas/'. $category->slug;
    ?>


<!-- The Link: Here we are assigning 'portfolio' to  'formVar' and setting the form 'action' to the $dest_url value. The title stuff is irrelevant here-->

    <a href="" onclick="document.post_form.formVar.value="portfolio"; document.post_form.action ='<?php $dest_url; ?>'; document.post_form.submit(); return false" title ="<?php sprintf( __( "View all posts in %s" ), $category->name ); ?>" >

Leave a Comment