Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show Custom Post Titles Based On a Selected Custom Taxonomy WordPress

  • SOLVED

Let me explain the problem I am dealing with:

I have 1 custom taxonomy named "province" and 1 custom-post-type named "pizzerie".
I need the taxonomy items displayed in a drop-down.

When I select a taxonomy item the second dropdown must display and links all the titles of the custom-post-type items based on what taxonomy item is selected. And when I select a title It will open the relative custom-post. It is similar to State and Cities. When I select the State in a first dropodown the relative Cities will display in a second dropdown.


Best regards, Antonio

Answers (20)

2012-06-30

Daniel Yoen answers:

try this :

<?php wp_dropdown_categories('taxonomy=custom_taxonomy_name'); ?>


Daniel Yoen comments:

Depends on your previous Post, you need this :


<form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>">
<div>
<input type="text" value="" name="s" id="s" />

<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a Category</option>'";

foreach($myterms as $term){
$term_taxonomy=$term->YOURTAXONOMY; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}

$taxonomies = array('YOURTAXONOMY');
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);

?>

<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>


natspace comments:

Well I insert your:

<script type="text/javascript">

$(document).ready(function(){

$("#json_options").change(function(){

var json_option = $("#json_options").val();


$("#json_post").empty().append("<option>loading...</option>"); //show loading...


var json_url = "<?php echo get_template_directory_uri(); ?>/index-json.php?json_option=" + encodeURIComponent(json_option);

$.getJSON(json_url,function(data){



$("#json_post").empty(); //clear states selections

if(data==""){

$("#json_post").append("<option value=\"0\">No states found</option>");

}

else{

for(i=0; i<data.id.length; i++){

$("#json_post").append("<option value=\"" + data.id[i] + "\">" + data.name[i] + "</option>");

}

}



});



return false;

});});

</script>





<?php

function get_terms_dropdown($taxonomies, $args)

{

$myterms = get_terms($taxonomies, $args);

foreach ($myterms as $term)

{

$term_taxonomy = $term->taxonomy;

$term_slug = $term->slug;

$term_name = $term->name;

$link = $term_slug;

$output .= "<option value=" . $term_slug . ">" . $term_name . "</option>";

}

return $output;

}

$taxonomies = array('province'); // REPLACE WITH YOUR OWN TAXONOMY

$args = array('orderby' => 'count','hide_empty' => true);

?>



<select name="json_options" id="json_options">

<option value="0">Select here</option>

<?php echo get_terms_dropdown($taxonomies, $args); ?>

</select>

<select name="json_post" id="json_post"></select>


In my custom template and add your index-json.php in my theme directory.

The first dropdown works fine, infact It displays all my taxonomy values. But when I selec a taxonomy value the second dropdown doesn't display the relative custom-post titles.

2012-06-30

Kailey Lampert answers:

I just tested this and it worked

function get_terms_dropdown() {
echo '<form class="searchcat" role="search" method="get" id="searchform" action="' . home_url() .'">';
echo'<div class="rowElem">';
echo'<input type="text" value="" name="s" id="s" placeholder="search here..." tabindex="1" />';
echo'<input type="submit" id="searchsubmit" value="Search" tabindex="2" />';
echo'</div></form>'; //form tag closes before <select>?

$taxonomies = array('pizzerie');//change Your_Texonomy to yours
$args = array('orderby'=>'count', 'hide_empty'=>true );
$myterms = get_terms( $taxonomies, $args );

$output = "<select>";

foreach($myterms as $term){

$term_taxonomy=$term->taxonomy;
$term_name =$term->name;
$link = get_term_link( $term, $term_taxonomy);
$output .= "<option value='{$link}'>{$term_name}</option>";

}

$output .= "</select>";
echo $output;
}


And could you clarify "nothing happens"? Does that really mean that <strong>nothing</strong> happens, or just that what you're expecting doesn't happen?

2012-06-30

Arnav Joy answers:

change here

$taxonomies = array('pizzerie');//change Your_Texonomy to yours


to

$taxonomies = array('category');//change Your_Texonomy to yours

check if it is called now


Arnav Joy comments:

one more thing

change hide_empty to false as follows:-

$args = array('orderby'=>'count','hide_empty'=>false)

so new function will look like..

function get_terms_dropdown() {

echo '<form class="searchcat" role="search" method="get" id="searchform" action="' . get_bloginfo('url') .'">';

echo'<div class="rowElem">';

echo'<input type="text" value="" name="s" id="s" placeholder="search here..." tabindex="1" />';

$taxonomies = array('pizzerie');//change Your_Texonomy to yours

$args = array('orderby'=>'count','hide_empty'=>false);

echo'<input type="submit" id="searchsubmit" value="Search" tabindex="2" />';

echo'</div></form>';

$myterms = get_terms($taxonomies, $args);

$output ="<select>";

foreach($myterms as $term){

$root_url = get_bloginfo('url');

$term_taxonomy=$term->taxonomy;

$term_slug=$term->slug;

$term_name =$term->name;

$link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;

$output .="<option value='".$link."'>".$term_name."</option>";

}

$output .="</select>";

echo ($output);

}

add_action('init', 'get_terms_dropdown');

2012-07-01

ewannerjose answers:

2012-07-01

udiscountmlb answers:

2012-07-01

180 180 answers:

2012-07-01

iontbrianhobe answers:

2012-07-02

kwronskishad answers:

2012-07-02

ehoggecris answers:

2012-07-02

lrchalkend answers:

2012-07-02

anhogganwesto answers:

2012-07-02

seaneswyno answers:

2012-07-02

soodpastephoe answers:

2012-07-02

nlandenmilan answers:

2012-07-02

simbishkell answers:

2012-07-02

oslemmonseste answers:

2012-07-02

trvantceci answers:

2012-07-02

ktigking answers:

2012-07-02
2012-07-02

ewithwedding answers: