WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] querying "products" custom post type stacked by "kind" taxonomies (10 posts)

  1. fpsdf
    Member
    Posted 2 years ago #

    Hello everyone,

    I need to make a page (well, not necessarily a wordpress default page) where i can see all the products (a custom post type i've made) ordered by kind (a taxonomy that i've also made).

    Like this:

    Pigments
    Pigmentos nacarados
    Pigmentos inorgânicos óxidos de ferro
    Pigmentos orgânicos
    AC Colours
    DC Colours
    IC Colours

    Colourants
    Ecotint / Econyl TS / Leomix
    Mecosperse
    Mecofast
    Corantes liquidos em DPM/PM GlobalTint
    Corantes universais C e SU GlobalTint

    Titaniums
    TiO2 R-403
    TiO2 R-2295
    R-244
    TiO2 R-298

    (oh, nevermind the language, it's portuguese)

    What would be the best technique?

    Thank you so much for your help!

  2. nprior
    Member
    Posted 2 years ago #

    Something like this:
    Create a copy of archive.php and call it archive-products.php.
    Edit achive-products.php and just before the end of the loop code like this:

    <?php
    $args = array('post_type' => 'products',
       'tax_query' => array (
          array ( 'taxonomy' => 'pigments',
             'field'    => 'id',
             'terms'    => get_the_ID()  )));
    $the_query = new WP_Query ($args);
    $i=0;
    while ( $the_query->have_posts() ) : $the_query->the_post();
          if ($i==0) { echo "<h3>Score(s)</h3>";  $i++; }
    	    the_title();
    endwhile;
    wp_reset_postdata();
    ?>

    I learnt this just 2 weeks ago and I'm passing on the karma ...!

  3. nprior
    Member
    Posted 2 years ago #

    No scrub that, I was solving another problem. Sorry!

  4. fpsdf
    Member
    Posted 2 years ago #

    no problem, nprior. I thank you anyway!

  5. nprior
    Member
    Posted 2 years ago #

    Take a look at creating taxonomy-kind.php (template hierarchy in the Docs), based on archive.php, then add this sort of code in just before the end of the loop:
    '<?php
    $args = array('post_type' => 'products',
    'tax_query' => array (
    array ( 'taxonomy' => 'kind',
    'field' => 'id',
    'terms' => get_the_ID() )));
    $the_query = new WP_Query ($args);
    while ( $the_query->have_posts() ) : $the_query->the_post(); the_title();
    endwhile;
    wp_reset_postdata();
    ?>'

  6. fpsdf
    Member
    Posted 2 years ago #

    for some reason, it is not using taxonomy-kind.php. It skips to index.php

  7. nprior
    Member
    Posted 2 years ago #

    http://wordpress.org/support/topic/taxonomy-template-file-in-custom-theme has a discussion about the taxonomy template. Some mixed experiences there.

    I'm trying some of this with a test site of mine. If I sort it out I'll report back.

  8. fpsdf
    Member
    Posted 2 years ago #

    you're very kind! thank you so much.
    please don't spend your working time because of me!

  9. fpsdf
    Member
    Posted 2 years ago #

    Anyway, i think this could work as a loop within a loop: the "kinds" would be looped and, within each "kind", respective "products" would be also looped.

  10. nprior
    Member
    Posted 2 years ago #

    I agree - I was hoping to find out how to make the taxonomy template work because that's where your "kind" would loop automatically.

    It might be worth checking out Rachel Carden's CPT-onomies plugin (who gave me that code in the first place) This effectively makes the taxonomy work more like a custom post type and there's a special archive slug which makes it easy to do what you wanted.

Topic Closed

This topic has been closed to new replies.

About this Topic