wp_query to display custom taxonomy terms

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.

Read More

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

Related posts

Leave a Reply

2 comments

  1. For a custom taxonomy query add the ‘tax_query’ => array()

    $args = array(
        'post_type' => 'featured_job',
        'post_status' => 'publish',
        'posts_per_page' => 9999999,
        'orderby' =>  'date',
        'order' => 'DES',
    
        'tax_query' => array(
            array(
                'taxonomy' => 'job_category', // custom taxonomy
                'field'    => 'slug',
                'terms'    =>  'business',    // taxonomy term (category)
            ),
        ),
    );
    
    
    
  2. *Moved comment to answer as suggested by @kaiser *

    Original comment accepted as answer

    If you are using the default wp template hierarchy (which it
    looks like you are) you won’t need any special WP query, WordPress
    will just know. So take a standard loop. Make a backup of that
    template, then add a normal WP look and see how you get on. It should
    be what you want.