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')
You should be able to accomplish that by modifying WP’s rewrite rules:
This is incomplete and you’ll need to change the target URL and rule itself, but this is the basis. Re-save your permalink settings once after implementing.
page
is the slug of the page to point to when the user access this URL (in this casedomain.com/page/remove/432
), and$matches[1]
should be the number afterremove/
in the URL. This number is accessible by the variable specified later,$query_vars[] = 'removeid';
/ ‘$removeid’ on the target page’s template will be the number in the URL, if specified.Ok so I chucked the rewrite all together and went with a $_POST solution. It works really well and no slop in my URL.
Put in Destination Page:
Put in Originating Page: