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

List custom taxonomy terms linking to specific custom post type WordPress

Hi All,

I've been going round in circles trying to figure out how to achieve the following as part of a custom theme I am developing using custom taxonomy 'mobile_phone' and various custom posts for news, specs, apps and reviews e.g. post type 'mobile_review'. I'd like a drop down that was structured as follows (indented for parent/child as hierarchical):

HTC (link to custom posts 'mobile_review' where custom taxonomy 'mobile_phone' = HTC)
Desire (link to custom posts 'mobile_review' where custom taxonomy 'mobile_phone' = Desire)
Wildfire (etc)
1800 etc etc

So I've tried to access all the custom posts of type 'mobile_review' but then output the associated terms (if get_terms is the correct function to use?) filtered by custom taxonomy. I then want to put these in a drop down menu for each section on my site: mobile specs / reviews / news (rather than the simple list format shown). So far I have..

function get_terms_by_post_type($taxonomies,$args){

$args = array(
'post_type' => 'mobile_review'

// The query for posts of type 'mobile_review'
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

//get terms
//$mobs = wp_get_object_terms($post->ID,"mobile_phone");
$mobs = get_terms('mobile_phone');
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";

echo "</ul>";


// Reset Post Data


I have managed to output posts by custom taxonomy and also achieve the above listing taxonomies but linking to all custom post types with given tax too. The problem is that following the link to 'HTC Desire' brings up news, specs and reviews using this tax term rather than just one custom post type. I can't for the life of me work out how to specify only one custom post type - assuming this is a custom query in functions.php.

Any help on this would be much appreciated. Thanks in advance!

Answers (2)


Utkarsh Kukreti answers:

What code did you use that didn't work? (for custom post + taxonomy link)


John Cotton answers:

Hi Paul

I'm not absolutely sure I understand your problem...are you saying you want a list of custom post types that have a specific taxonomy term?

If so, then something like this would work:

$query = new WP_Query( array( 'post_type' => 'mobile_review', 'mobile_phone' => 'htc-desire' ) );

Note that it's the term slug I use.

If I've misunderstood your problem, could you explain a bit further?


Paul Featherstone comments:

Thanks for the responses here.

This is the closest I have got to what I need, the problem with this code in terms of my requirements is that the posts returned are not limited to 'mobile_review' but all custom post types, also, the list is not hierarchical (i.e. children are not indented in the list):

//function in functions.php

function get_terms_dropdown($taxonomies, $args, $post_type){

$args = array(
'post_type' => 'mobile_review'
$the_query = new WP_Query( $args );

while ($the_query->have_posts()){


$myterms = get_terms('mobile_phone', 'orderby=count&hide_empty=1');//define variable to use 'the_terms()' function
$optionname = "mobile_phone";//name of taxonomy
$emptyvalue = "";//value
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a Mobile</option>'";

foreach($myterms as $term){//foreach loop for custom taxonomy list
$term_taxonomy=$term->mobile_phone;//specify which taxonomy to loop
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name." (".$term->count.") </option>";
//e.g. <option name='desire-hd' value='desire-hd'>Desire HD</option>
$output .="</select>";//concatenate closing tag for 'select' onto $output variable

return $output;//returns the drop down menu composed of three parts concatenated together


//output in page template

$taxonomies = array('mobile_phone');
$args = array();

echo get_terms_dropdown($taxonomies, $args, 'mobile_review');

John Cotton comments:


Forget the code for a minute - just explain as concisely as you can what you want to see...

From what you're saying, it's not the output above that's the issue, it's the page they link to that has the wrong content on. Is that correct?


Paul Featherstone comments:

Bit of both - output is not showing hierarchy of parents (mobile make) and child (mobile model) i.e. there is no indent. So the output is like:

Desire HD

The main issue is, as you say, that the page they link to has the wrong content, it displays all custom posts instead of the specific type I want (I want a query that I can change to display only one type of custom post only e.g. 'mobile_review').

Does this make sense?

Sorry, was responding to other request for code, don't want to confuse matters!

Thanks, paul.

John Cotton comments:

OK - now I understand.

Well, you've got quite a bit to do....!

[[LINK href=""]]wp_list_categories[[/LINK]] will give you the hierarchical list of your terms (assuming you've built them that way in the dashboard):

wp_list_categories( array( 'taxonomy' => 'mobile_phone') );

However, the link you'll get - as you've found - is a generic one for the term and the resulting page will, by default, show all posts that are tagged with that term.

To get around that, you'll need to customise the query on the term output page (pretty much as I should you in my first answer).

Assuming that when you show the mobile_phone tag you only ever want to show reviews, then you could create a taxonomy-mobile_phone.php file in your theme and just have this prior to the standard loop:

global $wp_query;
query_posts( array_merge( $wp_query->query, array( 'post_type' => 'mobile_review' ) ) );

(The merge is necessary to preserve the term tag coming in).

If, however, you want to <strong>sometimes </strong> have just reviews, but other times have everything, life gets more complicated since you'd have to pass in a variable to say which you wanted. The best way to do that would be to sub class the Walker_Category in wp_list_categories so that you could modify the link output to something like:

Then you'd need to have code in your taxonomy-mobile_phone.php to test for $_GET['t'] and adjust your query accordingly.