嵌套查询/多个元键内的排序

时间:2011-09-13 作者:Michelle

我有以下几乎完美的工作,但遇到了几个问题。

此时将显示一个Books archive页面,其顺序如下:

流派名称

--系列名称

---丛书,按丛书排序(又名阅读顺序)

这太棒了——我无法告诉你我花了多长时间才达到这一点!:)但是,我也希望能够确定每个流派中系列的排序顺序。这可能吗?现在看来,他们并没有按我所能控制的任何方式订货;它不是按日期或字母顺序出版的。

[侧栏:我还有一个问题,关于如何显示未分配“系列图书”元键的图书(它们将在一个称为“独立标题”的系列中,但管理员必须给它们系列编号是没有意义的)。我first asked that question here 但在我的完整代码的上下文中,它可能更有意义,所以我想交叉引用它。]

这是我的代码:

// 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 ?>
谢谢你的帮助!

2 个回复
最合适的回答,由SO网友:Michelle 整理而成

我用一个自定义的select查询解决了这个问题;以下是为子孙后代编写的完整代码。:)

<?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 ?>

SO网友:eddiemoya

这几乎与我在另一个问题上帮助别人锻炼的内容完全相同。

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

如果我错了,我误解了你的需要,请告诉我,我会尽力帮助你。

结束

相关推荐

display order of connections

是否有方法控制连接的显示顺序?我有一个Articles=>Books连接,这样在每篇文章中我都可以引用一些书。我想在文章的脚注中展示引用的书籍。目前,如果我创建三本书的连接,然后将它们显示在文章的底部,我将按相反的顺序获取它们。我能换一下吗?