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

Taxonomy list limited to a specific post type WordPress

  • SOLVED

I have two custom post types named "books" and "movies".

I have one custom taxonomy ("style") assigned to both these post types. Items in this taxonomy are e.g. "drama" and "comedy".

On a page called "books" I want to list the taxonomy items in a sidebar like this:

- Drama
- Comedy

They should link to a page that lists all items belonging to that taxonomy AND the relevant post type. In my example the first link should bring up an archive or taxonomy page that lists all items that are assigned to "drama" and have the post-type "books". Not the ones that belong to the post type "movies".

<strong>Bonus</strong>: a counter that only counts items that belong to that taxonomy item AND the post type:

- Drama (10)
- Comedy (6)

Note that it says "10" next to drama on the book page even if I had 20 movies labeled as "drama".

Answers (3)

2012-01-26

Milan Petrovic answers:

My GD Custom Posts and Taxonomies Tools Pro (http://d4p.me/gdtt) has this already implemented through a widget or a function. But, counter is a big problem.

To display number of posts, WordPress uses value stored in the database for each term, and it reflects all posts with that term regardless of post types. If you want counter that includes only specified post type(s), you would need to recalculate this value every time you need to show it, and that would be very slow.

2012-01-26

Ivaylo Draganov answers:

Hi,

I think there's no straight-forward way of achieving that in WP. It's better to have separate taxonomies for the different post types if you want to be able to count and list them separately. Refer to this older thread, there was a similar request:
[[LINK href="http://www.wpquestions.com/question/showLoggedIn/id/3159"]]http://www.wpquestions.com/question/showLoggedIn/id/3159[[/LINK]]

2012-01-26

John Cotton answers:

If you'd prefer a non-plugin solution, the easiest way to do this is with a custom permalink and a little bit of code in the taxonomy file.

So you've have a link /drama/books/ or /drama/movies and that would give you a list of just that type/tax. /drama would continue to you both.

If that's of interest, let me know and I will put up some code.


Achim Baur comments:

Yes, that would be awesome.


John Cotton comments:

OK - give me 20 minutes.


John Cotton comments:

To start with, it's best to have heirarchy file for the taxonomy - taxonomy-style.php.

If you've not got that, create it and copy in your archive.php (or whichever file has the layout that you want).

From you list of style terms on the dashboard, click view on drama or comedy to check that the output is coming from that new file.

If that's all working, we can move on.

Next some code for your functions.php file:

<code>
function my_add_rewrite_rules( $wp_rewrite ) {
$newrules = array();

// This rule assumes your current permalink for the taxonomy is /style. Change if that's not what you want.
$new_rules['^style/(.+)/(.+)'] = 'index.php?taxonomy=style&style=$matches[1]&post_type=$matches[2]';

$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'my_add_rewrite_rules');
<code>

Now some code for you taxonomy-style.php file. I assume you've got a "while( have_posts() )" line or similar in there. Just before that, add this:

<code>
global $wp_query;

$args = array_merge( $wp_query->query, array( 'post_type' => get_query_var('post_type') ) );

query_posts($args);
<code>

You should now just be seeing the posts of post_type in the style term set!

On the count, as others have said, you have to do it yourself.

The best way to create a function that you pass as the update_count_callback argument on register_taxonomy.

In that function you can run a little custom SQL that queries terms by post type and then stores that value in the options table to save having to generate it each time.

Let me know....


Achim Baur comments:

Thanks, I did get it to work after I've changed my permalink structure to the default setting.

The permalink structure I normally use is a custom one: "/%category%/%postname%"

I've tried but can't figure out how to change your code to work with my permalink structure. Can you help?


John Cotton comments:

It should work fine with that, that's what I use all the time....

Did I forget to tell you to resave you permalinks? I think I did....sorry.

Set them back to /%category%/%postname% and it will work just fine.

Let me know if not.


John Cotton comments:

It should work fine with that, that's what I use all the time....

Did I forget to tell you to resave you permalinks? I think I did....sorry.

Set them back to /%category%/%postname% and it will work just fine.

Let me know if not.


Achim Baur comments:

Ah, that did the trick!