I have a custom post type named ‘tours’ and custom taxonomy named ‘tourtypes’ so i can categorize it into terms like ‘cultural tour’, ‘sports tour’ etc.
I’m not the original author of this theme and don not know much about dealing with taxonomy. But I want to extend it so that instead of outputting all ‘tours’ post, user can also choose which tour category they want.
in the theme template taxonomy-tourtypes.php this is part of the code to display the term title which work:
<?php $terms = get_the_terms($post->ID, 'tourtypes'); foreach ($terms as $term){ ;?>
<h2><?php echo $term->name; ?> Category</h2><?php } ;?>
But when it comes to the part where i want to query the specified term it didnt work.
<?php
$paged = get_query_string_paged();
$counter = 1;
$termg = wp_get_post_terms($post->ID, 'tourtypes', array("fields" => "all"));
$posts_per_page = get_option('theme_show_portfolio_items');
if($posts_per_page == "") {
$posts_per_page = get_option('posts_per_page');
}
<?php
$paged = get_query_string_paged();
$counter = 1;
$termg = wp_get_post_terms($post->ID, 'tourtypes', array("fields" => "all"));
$posts_per_page = get_option('theme_show_portfolio_items');
if($posts_per_page == "") {
$posts_per_page = get_option('posts_per_page');
}
$my_query = new WP_Query(array('post_type' => 'tours', 'tourtypes' =>'$termg' ,'paged' => $paged, 'posts_per_page' => $posts_per_page));
...
I manage to get it to work if i type in the term slug eg.
$my_query = new WP_Query(array('post_type' => 'tours', 'tourtypes' =>'cultural' ...
I’m sure i just need a little change on the wp_query.
Anyone care to point to the right code..i almost give up with this
For a custom taxonomy query add the ‘tax_query’ => array()
*Moved comment to answer as suggested by @kaiser *
Original comment accepted as answer