Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We're building a site that sells Biking Gear

We're using the Genesis Theme Framework, and developing a custom child theme.

It has a Custom Post Type for Products, made with the "Custom Post Type UI Plugin".

It also has a custom taxonomy for product-categories, we've created a template for the custom post type taxonomy archive : taxonomy-products-category.php

The template needs to be able to loop through all the Posts in the product categories, but sort through them in term_order.

For Example, /product-categories/cycling/ needs to loop trough the child categories of 'Cycling' in the order we've set them up ( term_order, I think )

Also, if it's a child category, as seen in the menu on the left, the template just loops through that category, so if a user clicks on 'cycling', the template displays all the posts in the cycling category, grouped by sub-category ( jerseys, shorts, vests... ) and outputs the archive headline above each,

I'm at a loss for how to cleanly so this, I've tried cobbling together a number of snippets that has resulted in failure, and am turning to the community for advice.

share|improve this question

1 Answer 1

Here's what we've come up with so far, and there's definitely room for improvement.


remove_action( 'genesis_loop', 'genesis_do_loop' );

add_action( 'genesis_loop', 'ahso_custom_taxonomy_archive_loop' );

function ahso_custom_taxonomy_archive_loop() {

    global $wp_query; // globalize it.

    // get category ID
    $cat_id = $wp_query->get_queried_object_id(); // get current category

    // get all child categories
    $cat_args = array(
        'type'                     => 'products',
        'child_of'                 => $cat_id,
        //'parent'                   => '',
        'orderby'                  => 'term_group',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'product-categories',
        'pad_counts'               => false
    );

    // get all children of the current category
    $categories = get_categories( $cat_args );

    // if the cat has kittens, loop thru them in term order
    if ( !empty($categories) ) {

        foreach ($categories as $category) {

            // first output the category archive headline
            // gets the term table w/ the meta info added by genesis that contains the archive headline and intro text
            $term = get_term_by( 'id', $category->term_id, $category->taxonomy );

            echo '';
            echo ''.$term->meta["headline"].'';
            echo '';

            // then build a query to loop thru the posts in that cat

            // WP_Query arguments
            $args = array (
                'post_type'              => 'products',
                'order'                  => 'ASC',
                'orderby'                => 'menu_order',
                'tax_query'                 => array(
                    array(
                        'taxonomy'          => 'product-categories',
                        'field'             => 'id',
                        'terms'             => $category->term_id,
                        'include_children'  => false, // no kittens
                    )
                )
            );

            // loop thru posts in category
            // output featured image

            // The Query
            $cat_query = new WP_Query( $args );

            // The Loop
            if ( $cat_query->have_posts() ) {
                while ( $cat_query->have_posts() ) {
                    $cat_query->the_post();

                    // do something
                    echo '';
                    echo '';
                    echo the_post_thumbnail('medium');
                    echo '';
                    echo ''.get_the_title().'';
                    echo '';

                }
            } else {
                // no posts found
            }

            // Restore original Post Data
            wp_reset_postdata();

        }

    } // end category children loop

    // if the category has no children, just run a loop thru that category.
    // get_categories returns an empty array
    if ( empty($categories) ) {

        //echo 'this cat has no kittens';

        // WP_Query arguments
        $args = array (
            'post_type'              => 'products',
            'order'                  => 'ASC',
            'orderby'                => 'menu_order',
            'tax_query'                 => array(
                array(
                    'taxonomy'          => 'product-categories',
                    'field'             => 'id',
                    'terms'             => $cat_id, // get the initial $cat_id, from the query object
                    'include_children'  => false, // pls spay and neutr yrs pets
                )
            )
        );

        // The Query
        $cat_query = new WP_Query( $args );

        // The Loop
        if ( $cat_query->have_posts() ) {
            while ( $cat_query->have_posts() ) {
                $cat_query->the_post();

                // do something
                echo '';
                echo '';
                echo the_post_thumbnail('medium');
                echo '';
                echo ''.get_the_title().'';
                echo '';

            }
        } else {
            // no posts found
        }

        // Restore original Post Data
        wp_reset_postdata();


    } // end no children category loop

}

If you have any ideas, let us know. Feedback is always welcome.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.