I’m having trouble figuring out how to sort my custom post type, with 2 taxonomies.
Taxonomy 1) Location (ie. Toronto, New York) - slug: location Taxonomy 2) Trade (ie. Carpenters, Electricians) - slug: trade
When viewing the Toronto page (using my taxonomy template: taxonomy-location.php), I’d like to be able to sort all of the trades that are listed in Toronto. It should look something like this:
——
Toronto (Page title)
Carpenters
Carpenter 1 Name, Carpenter 2 Name, Carpenter 3 Name, Carpenter 4 Name
Electricians
Electrician 1 Name, Electrician 2 Name, Electrician 3 Name, Electrician 4 Name
——
I’m creating the query loop within my custom taxonomy template (taxonomy-location.php), so I assumed the posts that show up would already be tagged in the Toronto location, but this doesn’t seem to be the case.
Here’s the loop from my taxonomy-location.php file:
<?php
$args=array(
‘trade’ => ‘carpenters’,
'post_type' => 'post_type_name',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Carpenters';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
If I add another argument for the location taxonomy to the first array, it works – but I don’t want it to be restricted to toronto. I want it to pull from whatever location page you’re viewing (ie. new york, toronto, etc).
<?php
$args=array(
‘trade’ => ‘carpenters’,
'location' => ‘toronto’, // this should be something like this <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?>
'post_type' => 'post_type_name',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Carpenters';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
What’s the best way to accomplish this?
You can use get_query_var
for location but you must first register it for Wordpress to have access. You have to do this for all custom variables:
function add_query_vars_filter( $vars ){
$vars[] = "location";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );