Preface:
I've read a lot of requests for using multiple taxonomy term names as view arguments. I found a code, but it didnt work for me.
Views default argument handling using termid rather then name works beautifully, using forms like: "mydomain.com/viewpage/termid1,termid3,termid2" or "mydomain.com/viewpage/termid1+termid2+termid3", but I found the support for term names lacking.
Concept:
- What I did with my handling code was basically grab the arguments array from mydomain.com/viewpage/term1/term2/term3, then convert any string arguments to a termid, and then flatten the arguments array (which is now all term id's) to the form of "termid1,termid2,termid3", so I can take advantage of the great termid argument support.
Setup
- Setup a view with ONE argument field of type: Taxonomy Term ID Notice it is term ID NOT term name
- Paste Code below into Argument handling Code
- browse your view with: mydomain.com/viewpage/termname1/termname2/termname3
NOTE: with this setup, you can use term id's as well as termnames at the same time, and in any order you want!
Code:
// Blake Edwards Arugment Handling Code
// - Multiple Taxonomy Term Names as Arguments
$exclude_vocab = array('13');
if ($args[0]):
//replace underscores with spaces
foreach ($args as $key=>$arg) { $args[$key] = str_replace('_',' ',$arg); }
$args_original = $args;
$args_processed = array();
foreach ($args as $key=>$arg)
{
if (!is_numeric($arg) || strlen($arg) == 4)
{
$terms = taxonomy_get_term_by_name($arg); //do a search for terms, return term objects
if (count($terms) > 1)
{
//Exclude Vocabularies
foreach ($terms as $key=>$term) { if (in_array($term->vid,$exclude_vocab)) { unset($terms[$key]); } }
}
array_push($args_processed,$terms[0]->tid);
}
else { array_push($args_processed,$arg); }
}
//Flatten Array to one Argument Comma Separated
foreach ($args_processed as $arg) { $newargs .= "$arg,"; }
$newargs = substr($newargs,0,-1);
return array($newargs);
ENDIF;