Nirjhar Mandal
Nirjhar Mandal

Reputation: 227

content searching by custom taxonomies in wordpress

I made some custom taxonomies in my wordpress site and now I want to search the particular taxonomy related content and display these by selecting the taxonomies option from drop down list.

Suppose One of my taxonomies name is "metal". then I made this as drop down by following code:

wp_dropdown_categories( 'taxonomy=metal' );

But I don't know how to search the related content in my site by selecting metal list from drop down bar. Is there any appropriate solution or plugin for that?

Thanks.

Upvotes: 2

Views: 211

Answers (1)

Vijaya Narayanasamy
Vijaya Narayanasamy

Reputation: 468

Try the below code.It worked for me:

<form action="" method="POST">
        <?php $arr = array('taxonomy'=> 'metal','hierarchical' => 'true','show_count' => 'true','hide_empty'=> '0', 'selected' =>  $kat = get_query_var( 'cat' ), 'name' => 'cat', 'id' => '', 'posts_per_page' => -1, 'echo' => 0); ?>

        <div class="dep_list">
            <label>Select a department:</label><?php $select = wp_dropdown_categories($arr); ?>
            <?php $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
            echo $select; ?>
        </div>
</form>

<?php
  $args = array(
  'post_type' => 'your post type slug',
   'tax_query' => array( array( 'taxonomy' => 'metal', 'terms' => array($kat), 'field' => 'id') ) );

   $my_query = new WP_Query( $args );
   echo '<div class="row">';
   if( $my_query->have_posts() ) { 
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <h4><?php echo the_title(); ?></h4>
            <p><?php echo the_content(); ?></p>

    </div>

  <?php endwhile;
}?>

Upvotes: 1

Related Questions