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.

I have the following ALMOST working perfectly, but have run into a couple of issues.

Right now this displays a Books archive page, ordered like this:

Genre Name

-- Series Name

--- Books within series, sorted by book-in-series (aka reading order)

Which is fantastic - I can't tell you how long it's taken me to get to this point! :) But, I would also like to be able to determine sort order for the Series within each Genre. Is this possible? Right now it does not seem they are ordering by anything I can control; it's not by published-on date or alphabetically for instance.

[SIDEBAR: I also have a question on how to display books which have not been assigned a "book in series" meta-key (they will be within a series called "Stand Alone Titles" but it doesn't make sense for the admins to have to give them book-in-series numbers). I first asked that question here but it might make more sense within the context of my full code, so I wanted to cross-reference it.]

Here's my code:

// QUERY THAT PULLS ALL SERIES SORTED BY GENRE
$args = array( 
    'post_type' => 'series', 
    'posts_per_page' => -1,
    'nopaging' => true,
    'surpress_filters' => true,
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'meta_key' => 'genres',
); 

$series = new WP_Query( $args);

    if( $series->have_posts() ) : ?>

    <?php
        // ESTABLISH PREVIOUS GENRE AS SOMETHING IMPOSSIBLE (NOT BLANK)
        $previous_genre = 'dfasd33';

        while( $series->have_posts() ) : $series->the_post();
            // Get custom post data to set current genre
            $current_genre =  get_post_meta( get_the_ID(), 'genres', true );
            if( $current_genre != $previous_genre ) {
                echo '<h2>' . stripslashes($current_genre) . '</h2>';
            }
            $previous_genre = $current_genre; 
        ?>
            <!-- DISPLAY THE SERIES NAME AND DESCRIPTION -->
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php the_content(); ?>

            <?php

                $current_series_id = $series->post->ID;

                // QUERY THAT PULLS ALL BOOKS WITH PARENT ID OF CURRENT SERIES SORTED BY BOOK-IN-SERIES
                $args = array( 
                    'post_type' => 'books', 
                    'posts_per_page' => -1,
                    'nopaging' => true,
                    'surpress_filters' => true,
                    'orderby' => 'meta_value',
                    'meta_key' => 'book_in_series',
                    'order' => 'ASC',
                    'post_parent' => $current_series_id,
                );

                $books = new WP_Query( $args);

                    if( $books->have_posts() ) : 

                    while( $books->have_posts() ) : $books->the_post(); ?>
                        <h4><?php the_title(); ?></h4>

                    <?php endwhile; //end book query while statement ?>
                    <?php endif; // end book query if statement ?>

            <?php endwhile; // end series query while statement ?>

<?php endif; // end series query if statement ?>

Thanks for your help!

share|improve this question

2 Answers 2

This is almost exactly the same as what I am helping someone workout on another question.

Way to include posts both with & without certain meta_key in args for wp_query?

If I'm wrong and I have misunderstood what you need, let me know and I'll try to help.

share|improve this answer
    
That's me too :-) - This is a slightly different question, on how to order the nested elements. See the paragraph that begins "Which is fantastic - " above for this question. I'm going to try your suggestion on the other shortly. thanks!! Michelle –  Michelle Sep 13 '11 at 22:22
    
Are you just trying to sort the items in the inner query? If so its not going to be any different than a normal orderby. codex.wordpress.org/Class_Reference/… You can orderby author, publish date, etc,.. a whole slurry of things. If you need to order by meta key again, then its going to be some variation of whatever we figure out in the other post - depending on what your trying to do differently here. Overall I guess on not clear on what your trying to do, and not sure why you have two queries. –  eddiemoya Sep 13 '11 at 23:23
    
Actually I'm trying to sub-sort the outer query, which currently returns the custom post type "Series" sorted by meta_key "genre". This successfully groups all "historical" Series together, for instance, but I'm looking for a way to sub-sort within that list. The inner query is the one we've been working on in the other question, custom post type "Books" sorted by meta_key book_in_series. (PS wish I could vote you up multiple times for all the help you've been giving me -thank you!!) –  Michelle Sep 13 '11 at 23:53
up vote 0 down vote accepted

I've solved this with a custom select query; here's the full code for posterity. :)

<?php               
// QUERY THAT PULLS ALL SERIES SORTED BY GENRE, THEN MENU ORDER
$querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->postmeta.meta_key = 'genres' 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'series'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->postmeta.meta_value DESC, $wpdb->posts.menu_order ASC
 ";

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

    if( $series ) : 
    global $post;  ?>

    <?php
        // ESTABLISH PREVIOUS GENRE AS SOMETHING IMPOSSIBLE (NOT BLANK)
        $previous_genre = 'dfasd33';

        foreach ($series as $post):
        setup_postdata($post);
            // Get custom post data to set current genre
            $current_genre =  get_post_meta( get_the_ID(), 'genres', true );
            if( $current_genre != $previous_genre ) {
                echo '<h2>' . stripslashes($current_genre) . '</h2>';
            }
            $previous_genre = $current_genre; 
        ?>
            <!-- DISPLAY THE SERIES NAME AND DESCRIPTION -->
            <h3 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

            <!-- DISPLAY SERIES CONTENT AREA, IF CONTENT ISN'T EMPTY -->
            <?php $cc = get_the_content(); ?>
            <?php if($cc !== "") { ?>
                <div class="seriesdesc"><?php the_content(); ?></div><!-- .seriesdesc -->
            <?php } ?>

                <?php
                $current_series_id = get_the_ID();

                // QUERY THAT PULLS ALL BOOKS WITH PARENT ID OF CURRENT SERIES SORTED BY BOOK-IN-SERIES
                $args = array( 
                    'post_type' => 'books', 
                    'posts_per_page' => -1,
                    'nopaging' => true,
                    'surpress_filters' => true,
                    'orderby' => 'meta_value',
                    'meta_key' => 'book_in_series',
                    'order' => 'ASC',
                    'post_parent' => $current_series_id,
                );

                $books = new WP_Query( $args);

                    if( $books->have_posts() ) : ?> 
                        <div class="booklist">
                    <?php while( $books->have_posts() ) : $books->the_post(); ?>

                        <?php if (function_exists('vp_get_thumb_url')) {
                                // Set the desired image size. Swap out 'thumbnail' for 'medium', 'large', or custom size
                                $thumb=vp_get_thumb_url($post->post_content, 'medium');

                                if ($thumb!='') { ?>
                                    <div class="archivecover">
                                        <a href="<?php echo get_permalink(); ?>"><img src="<?php echo $thumb; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
                                    </div><!-- .cover -->
                                <?php }
                        } ?>

                    <?php endwhile; //end book query while statement ?>
                    </div><!-- .booklist -->

                    <?php endif; // end book query if statement ?>

            <?php endforeach; // end series query foreach statement  ?>

<?php endif; // end series query if statement ?>
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.