• Resolved nvd80

    (@nvd80)


    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.

    Need to tell u that i’m not the original author of this theme and i try to customise 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:

    <?php $terms = get_the_terms($post->ID, 'tourtypes'); foreach ($terms as $term){ ;?>
    <h2><?php echo $term->name; ?> Category</h2><?php } ;?>

    but i’m not sure whats wrong when trying to query the selected term

    <?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));
    	if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    		<?php $image = get_post_thumb($post->ID,280,180); ?>
    			<li class="image">
    			<a href="<?php the_permalink();?>"><img src="<?php echo $image['src'];?>" alt="<?php the_title();?>" /></a>
    			<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
    			<?php
    				global $more;    // Declare global $more (before the loop).
    				$more = 0;       // Set (inside the loop) to display content above the more tag.
    			?>
    			<?php add_filter('the_content', 'remove_images'); ?>
    			<?php add_filter('the_content', 'remove_objects'); ?>
                <?php the_meta(); ?>
    			<p><?php the_content(__('Book Now' , 'oxygen')); ?></p>
    			</li>
    
    			<?php if($counter%3 == 0) { ?>
    					<li class="spacer"> </li>
    				<?php } ?>
    				<?php $counter++;?>
    
    				<?php endwhile; else: ?>
    				<?php endif; ?>

    I think i dont use proper code at the wp_query. anybody can point me to the right code?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Function wp_get_post_terms() with ‘fields’ => ‘all’ will return an array of term objects.

    I believe that you need to change the query from this:

    $my_query = new WP_Query(array('post_type' => 'tours', 'tourtypes' =>'$termg' ,'paged' => $paged, 'posts_per_page' => $posts_per_page));

    to this:

    $my_query = new WP_Query(array('post_type' => 'tours', 'tourtypes' =>$termg[0]->name ,'paged' => $paged, 'posts_per_page' => $posts_per_page));
    Thread Starter nvd80

    (@nvd80)

    Thanks a lot! I knew i almost there but couldnt figure out about the array stuff.

    If your problem has been solved, please use the dropdown on the right to mark this topic ‘Resolved’ so that anyone else with this question can see that there is a solution.

    Thread Starter nvd80

    (@nvd80)

    Hopefully others can also find a good reference here.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘wp_query for custom taxonomy term’ is closed to new replies.