Single Taxonomy Attached to Multiple Post Types Template Question

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%.

Read More

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')

Related posts

Leave a Reply

2 comments

  1. You should be able to accomplish that by modifying WP’s rewrite rules:

    add_action( 'init', 'ss_permalinks' );
    function ss_permalinks() {
        add_rewrite_rule(
            'page/remove/([^/]+)/?',
            'index.php?pagename=page&service=$matches[1]',
            'top'
    );
    }
    add_filter( 'query_vars', 'ss_query_vars' );
    function ss_query_vars( $query_vars ) {
        $query_vars[] = 'removeid';
        return $query_vars;
    }
    

    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 case domain.com/page/remove/432), and $matches[1] should be the number after remove/ 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.

  2. 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:

        <?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 ); ?>" >