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

custom taxonomy template page issues WordPress

  • SOLVED

Warning: (lol)
If you have not worked with custom post-types in wp, alongside custom taxonomies, don't bother with this question. You will end up going in circles for days like I have.

Situation:
I have a custom post-type 'photogalleries' which I have also created 2 custom taxonomies for it, 'gallerycategories, gallery-tags'.

gallerycategories is heirarchical, while gallery-tags is not.

Post-type and taxonomies are registered and working perfectly. Both single and archive pages work for the photogalleries post-type.


Problem:
When I click on a taxonomy in my photogallery page, it takes me to a page showing all the photogalleries. IT SHOULD only show photogallery posts that belong to that taxonomy.

example single gallery:
[[LINK href="http://shawn.theanointedone.com/photogallery/amazing-pet-pictures/"]]http://shawn.theanointedone.com/photogallery/amazing-pet-pictures/[[/LINK]]

Right below the gallery title is the 'gallerycategories' taxonomy, in this case it is pets.

I created a custom template for gallerycategories which generates this page:
[[LINK href="http://shawn.theanointedone.com/gallerycategories/pets/"]]http://shawn.theanointedone.com/gallerycategories/pets/[[/LINK]]

The problem is all photogalleries are showing up, and not just 'pets'.


Problem 2:
Same thing happens for my gallery-tags.

The only difference is this time I did not create a template for tags, as there is no point, as I don't know what code to put in the template to make it work anyhow, should be same code as the first one anyhow.



Here is the code from my taxonomy-gallerycategories.php file
[[LINK href="http://pastebin.com/1Y7dyMy1"]]http://pastebin.com/1Y7dyMy1[[/LINK]]

Here is the code that generates the links to the gallerycategories terms on the single.php page:
<?php echo get_the_term_list( $post->ID, 'gallerycategories', '', ', ', '-' ); ?>

What I need:

I need the function to place into taxonomy-gallerycategories.php that is going to properly filter the displayed articles to only show the photogallery articles that belong to the 'gallerycategories' taxonomy term --determined via the url of course.


I'm guessing this problem is pretty easy to solve, but with no documentation yet, it's a serous pain in my butt.

Answers (3)

2010-11-10

Michael Fields answers:

Shawn, I don't believe that your template needs the following two lines:
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
<?php query_posts(array( 'post_type'=>'photogallery', 'location'=>$term->slug)); ?>


WordPress will automatically display terms for the queried (via url) taxonomy + term.

The use of query_posts() to restrict post_type is not necessary. In my experience, it is best only to use query_posts() as a last resort or in special cases. IMO this is not one of them.

Here is what one of the queries generate by wp() looks like on my test blog:

SELECT SQL_CALC_FOUND_ROWS wp_posts.*
FROM wp_posts
WHERE 1=1
AND wp_posts.ID IN (546, 547)
AND wp_posts.post_type IN ('post', 'page', 'attachment', 'piece')
AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1
AND wp_posts.post_status = 'private')
ORDER BY wp_posts.post_date DESC
LIMIT 0, 3


My custom taxonomy is "piece" and you can see that it is included in the query automatically.

Removing the 2 aforementioned lines fixed the template for me.


shawn comments:

When I remove those 2 lines, which I just did again, I always get 'Sorry, no posts matched your criteria.'

I believe this is because the query is searching 'posts' and not my custom post-type, but that is only my best guess.

It's when I am combining custom post-type and custom taxonomies together to search via term, that I run into issues. Believe me... I've read every article I can find on this, and none seems to quite get there.

**If it helps, you are the one person who I fully trust to give full access to the site to if it makes it easier**


Michael Fields comments:

I can take a look if you would like me to. It is possible that this is a Woo specific issue. Before we get there, could you post the code that you used to create the taxonomy + post type. Through debugging various custom post_type related things I have realized that sometimes the error lies there and not so much in the template files.


shawn comments:

[[LINK href="http://pastebin.com/E4X2S4Rh"]]http://pastebin.com/E4X2S4Rh[[/LINK]]

I use this class because it is the best out there for allowing addition of the post-type archive pages rewrites until 3.1 comes out. Without that, I have found no way to activate the arhive files for the post-type.

When I dig into the db, the post-type is indeed created properly, but then again you can see that from the function itself, which btw is pretty cool


Michael Fields comments:

Shawn,

You should remove this line from your post type declaration in functions.php:

'exclude_from_search' => 'false',

If I were you, I would remove most of the args that you have declared only setting those that need to be set.

Then you should be able to remove the 2 lines from your template.


shawn comments:

U DA MAN!

Figured out that I only needed to take out the query line.

I have to leave the other line in to have the ability to display the term name in my page. When I remove the $term = line then there is nothing defined for my query.

I do have one last question if you don't mind, -- you win either way...

I have the following line in my template, that is intended to show the description of the term on the page:

<strong>Description:</strong><?php echo term_description( '', get_query_var( 'gallerycategories' ) ); ?>

This is not returning the description for my term, but it seems that it should.

What query would you use?

**$term is defined because you can now see on the top of the page:
[[LINK href="http://shawn.theanointedone.com/gallerycategories/pets/"]]http://shawn.theanointedone.com/gallerycategories/pets/[[/LINK]]

Photogalleries in Category: Pets ****It knows that we're on the pets term page.


Michael Fields comments:

Sweet, glad it worked!

Since you're in a term archive, the following is the easiest solution:

term_description( get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

Will probably not work in other templates.


shawn comments:

Yeah, didn't work. Tried with both echo and without.. will keep digging.

<?php echo term_description( get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>

Setting this response as the winner now.

thanks so much yet again.

This theme project is finally coming to a close and I think it looks pretty good. If I have further questions, I'll keep coming back here to post. This site and members totally rock.

2010-11-10

Monster Coder answers:

Hello,
I am also not 100% confident with this. Recently I am trying to understand it! My answers are more for my understanding rather than helping you ;)!

First, when you created the link for Pets, did you create the link using
get_term_link($term, $taxonomy)

I have another question, let me do the homework first :P.


shawn comments:

<?php echo get_the_term_list( $post->ID, 'gallerycategories', '', ', ', '-' ); ?>


Monster Coder comments:

Ok, that seems ok too! As only one showing, I thought you hard coded it :P! I am updating soon, if I find something to share!

2010-11-10

idt answers:

Hi Shawn,

Have you seen this post by Justin Tadlock here: http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database

idt


shawn comments:

Yes, read that article when it first came out. He does not even attempt what I am after.


idt comments:

I see. I thought that the part about "Displaying taxonomy terms in a page" was the same as what you're trying to do, no? I might have misunderstood what you're trying to do.