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

Show custom posts by category - but sort by subcategory WordPress

  • SOLVED

I have made a custom post type (for my products) and a corresponding hierarchical taxonomy (a catalog for my products).

I have renamed category.php to taxonomy.php. It now shows all my products that belong to a certain category.

Now, since I have a lot of products, I don't want all of them listed on that page, I'd rather have them grouped by subcategory.

Let me give you an example: Say I sell clothing and my taxonomy looks like this:

- Pants
- - Loose Fit
- - Baggy Style
- Shoes
- - Leather
- - Suede

When taxonomy.php is called to show all pants, I'd like to have them listed like this:

- Loose Fit
- - Loose Pants #1
- - Loose Pants #2
(...)
- Baggy Style
- - Baggy Pants #1
- - Baggy Pants #2
(...)

Answers (4)

2010-10-01

Ivaylo Draganov answers:

Hello,

I think I've cracked that. It's possible to query by custom taxonomies - it's just has not been documented yet. OK, I hope I've understood the question right. Here we go.

I've used the default TwentyTen theme with a custom taxonomy called "Catalog" and post type called "Products".

1) Put this in functions.php

// register custom post types and taxonomies
function register_custom_post_types() {

// register custom post type
$labels = array(
'name' => 'Products',
'singular_name' => 'Product'
);

$args = array(
'labels' => $labels,
'public' => true,
'capability_type' => 'post',
);

register_post_type('products', $args);

// register taxonomy
$labels = array(
'name' => 'Catalogs',
'singular_name' => 'Catalog',
);

register_taxonomy('catalog', array('products'), array(
'hierarchical' => true,
'labels' => $labels,
'public' => true
)
);

}
add_action('init', 'register_custom_post_types');


2) Put this in your taxonomy-catalog.php

<?php
// taxonomy-catalog.php

// set variables
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$tax = get_query_var( 'taxonomy' );

get_header(); ?>

<div id="container">
<div id="content" role="main">

<h1 class="page-title"><?php echo $term->name; ?></h1>
<?php

// if term has children
if (get_term_children($term->term_id, $tax) != null) {

// get child terms
$term_children = get_terms(
$tax,
array(
'child_of' => $term->term_id,
)
);

// loop through them
foreach ($term_children as $term_child) {

// print title
echo '<h2>' . $term_child->name . '</h2>';

// change query
query_posts(array(
'catalog' => $term_child->slug,
)
);

// loop through posts
if (have_posts()) : ?>

<ul>

<?php while (have_posts()) : the_post(); ?>

<li <?php post_class(); ?>>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

</li>

<?php endwhile; ?>

</ul>

<?php endif;

}

// if term has no chilren
} else {

// loop through posts
if (have_posts()) : ?>

<ul>

<?php while (have_posts()) : the_post(); ?>

<li <?php post_class(); ?>>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

</li>

<?php endwhile; ?>

</ul>

<?php endif;

}

?>

</div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>


3) Enjoy

Please check out the result on my test site:
http://test.druuf.com/wordpress/?catalogs=pants (parent category)
http://test.druuf.com/wordpress/?catalogs=baggy-style (child category)


Achim Baur comments:

Awesome, you've really cracked it. Thanks a bunch!

2010-10-01

Denzel Chia answers:

Hi,

You can further classified them using templates according to WordPress template hierarchy.

<blockquote>taxonomy-{taxonomy}-{term}.php - If the taxonomy were sometax, and taxonomy's slug were someterm WordPress would look for taxonomy-sometax-someterm.php
taxonomy-{taxonomy}.php - If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php</blockquote>


Thanks.


Denzel Chia comments:

You may need to break your taxonomy into a few, not one taxonomy product in order to achieve what you want.

You will have to register pants taxonomy, under it create a term loosefit and create a template taxonomy-pants-loosefit.php, so that it will show loose pants 1, and loose pants 2 etc.

Hope it helps.

Thanks.


Achim Baur comments:

I don't really want to introduce multiple taxonomies.

2010-10-01

Cosmin Popovici answers:

I think you are in the situation to query posts by custom taxonomy... and that's not in WordPress, yet.

You can do it with a custom select query. Something like this:


$querystr = "
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'pants'
AND $wpdb->terms.slug = 'loose-fit' OR $wpdb->terms.slug = 'baggy-style'
ORDER BY $wpdb->posts.post_date DESC
";

$pageposts = $wpdb->get_results($querystr, OBJECT);



And that is just for 2 terms... You then need to use a different loop; something like explained [[LINK href="http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#The_Revised_Loop"]]here[[/LINK]].

Or, you can assign the default "category" taxonomy for your custom post type. Also, you won't have the custom taxonomy slug in the URL if you define your own taxonomy (something I discussed [[LINK href="http://themeforest.net/forums/thread/custom-post-typetaxonomy-permalinks/31585"]]here[[/LINK]])

Hope I understood your issue and this helps :)


Achim Baur comments:

Not sure if I understand your answer.

I use taxonomy.php to list all my posts by custom taxonomy. Works fine. All my pants are listed. I want to keep this method, it's very useful for me. I can make a side menu that lists my custom categories, you click on 'pants', you see all the pants. Works great, it's just a very long list. I can sort it alphabetically or by date - but that does not really help the user.

I would need code that checks if there are subcategories to the current category and then group the pants by that subcategory. First the loose pants, then the baggy pants and so on. Much easier on the eyes.

Right now I use the standard loop.php that came with Twenty Ten, I've just renamed it to loop-taxonomy.php. I think I need some code there that groups my posts by subcategory. Is this possible?

2010-10-01

Rok Don answers:

Its simple.
even you can use your category.php file for your purpose.
you just need to do setting in admin.
it will display product only for selected category or subcategory whatever.
Even you can also change the URL structure to your category and sub category name


Achim Baur comments:

I am not sure, I understand your answer. Could you elaborate?


Rok Don comments:

can you give me access of your admin and frontend , i will solve your problem