0

This is a really neat idea but I'd like to expand upon it. In his post, David Kennedy writes out some code to allow a basic wp_list_pages type feature for Custom Post Types to use as a sitemap. His code is as follows:

<h2 id="posts">My Post Type</h2>
    <ul>
    <?php 
        $terms = get_terms( 'my_taxonomy', 'orderby=name' );
        foreach ($terms as $term) {
            echo "<li><h3>".$term->name."</h3>";
            echo "<ul>";
            $args = array(
                'post_type' => 'my_posttype',
                'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'my_taxonomy',
                        'field' => 'slug',
                        'terms' => $term->slug
                    )
                )
            );
            $new = new WP_Query($args);
                while ($new->have_posts()) {
                    $new->the_post();
                    echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
                }
            echo "</ul>";
            echo "</li>";
        } ?>
        </ul>

I like this idea but am wanting to take it a bit further. It'd be great to add depth, or levels of hierarchy, and to show parent/child relationship results instead of listing Custom Post Types alphabetically. In other words, mimic more of the functions that wp_list_pages offers.

Is there an easier way to do this or is there functionality in WordPress that already does what I'm looking for?

1 Answer 1

1

Here is a quick crack at it, which should work in two level depth:

<h2 id="posts">My Post Type</h2>
<ul>
<?php
    $not_in = array(); //to avoid naming the same post over and over
    //get top level terms
    $Parent_terms = get_terms( 'my_taxonomy', array('orderby' => 'name','parent' => 0));
    foreach ($Parent_terms as $term) {
        echo "<li><h3>".$term->name."</h3>";
        echo "<ul>";
        //get all children of each term
        $child_terms = get_terms( 'my_taxonomy', array('orderby' => 'name','parent' => $term->ID));
        foreach ((array)$child_terms as $t){
            echo "<li><h4>".$t->name."</h4>";
            echo "<ul>";
            $args = array(
                'post__not_in' => $not_in,
                'post_type' => 'my_posttype',
                'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'my_taxonomy',
                        'field' => 'slug',
                        'terms' => $t->slug
                    )
                )
            );
            $new = new WP_Query($args);
            while ($new->have_posts()) {
                $new->the_post();
                $not_in[] = $post->ID;
                echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
                //here you should check for children posts and output them 
            }
            echo "</ul>";
            echo "</li>";
        }
        //get all posts that are only listed in top level term
        $args = array(
            'post__not_in' => $not_in,
            'post_type' => 'my_posttype',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'my_taxonomy',
                    'field' => 'slug',
                    'terms' => $term->slug
                    )
                )
        );
        $new = new WP_Query($args);
        while ($new->have_posts()) {
            $new->the_post();
            $not_in[] = $post->ID;
            echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
            //here you should check for children posts and output them 
        }
        echo "</ul>";
        echo "</li>";
    } ?>
 </ul>
13
  • Neat, although I can't seem to get this working correctly when changing the proper values as reflected in David's post. I'll keep playing around and seeing if I can make this page work with the given code and give you my feedback. Also, if I wanted to add a third level depth, what would be required?
    – micah
    Commented Sep 13, 2011 at 23:47
  • It appears the problems in your code lie with the get_terms parameter where parent is equal to a value. I used a PHP validator to show me any errors and fixing those two lines allowed the page to at least show up, but it's not populating with any of my Custom Post Type posts.
    – micah
    Commented Sep 14, 2011 at 5:50
  • Create a pastebin of the code you are using and ill take a look.
    – Bainternet
    Commented Sep 14, 2011 at 6:04
  • Oh, actually, I made no significant changes to your code. All I did was change to 'parent=0' in both lines, which allowed the page to render the code and show something. Nothing else about your version of the code changed.
    – micah
    Commented Sep 14, 2011 at 6:12
  • you can't use part array and part string for arguments so it makes no sense, no I've just check it it seems to work fine so if you want, show me your exact code and i'll see whats wrong
    – Bainternet
    Commented Sep 14, 2011 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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