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')
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:
Put in Originating Page: