TAX_QUERY中的ORDER或ORDERBY(如何在WP_QUERY中定义术语的顺序)

时间:2016-06-29 作者:tmgale12

我正在尝试解决如何在WP\\u查询中定义术语的顺序。

我正在创建一个知识库。我有一个称为知识库的CPT和一个称为主题的分类法。

我的代码输出所有主题术语,然后循环浏览4篇具有元键top\\u four\\u num的帖子,这是为了显示主题中的前四个问题。然后我有一个嵌套的循环,该主题中的其余帖子都是这样的。这些都被JQUERY隐藏,用户单击“显示所有链接”以显示。

一切都按我所希望的方式进行,但我不知道如何改变条款的顺序。目前,它们按字母顺序出现。

我已向codex和tax\\u查询中没有基于订单的参数。有人能告诉我如何控制tax\\u查询顺序吗?

我的代码如下。非常感谢您的帮助!

        <?php

    /**
     * Knowledge Base        
     */

    //Call in the header
    get_header();

    //* Customize search form input box text
    add_filter( \'genesis_search_text\', \'sp_search_text\' );
    function sp_search_text( $text ) {
        return esc_attr( \'Ask us a question...\' );
    }

    // Remove div.site-inner\'s div.wrap
    add_filter( \'genesis_structural_wrap-site-inner\', \'__return_empty_string\' );

    //* Add custom body class to the head
    add_filter( \'body_class\', \'sp_body_class\' );
    function sp_body_class( $classes ) {

            $classes[] = \'KB\';
            return $classes;
    } ?>

    <style>.site-inner {
        max-width: none!important;
        padding-top: 0;
    } </style>


    <!-- Top Banner -->
    <div style = "text-align: center; " class = "top-banner">
    <h1 style = "color: white;">Knowledge base showcase</h1>
            <?php get_search_form(); ?>
    </div>
        <!--body class-->
    <div class = "body-kb">

    <?php
    // Begin Main Topic loop
    $_terms = get_terms( array(\'topic\') );

    foreach ($_terms as $term) :
    $ids = get_field(\'top_four\', false, false);
        $term_slug = $term->slug;
        $_first = new WP_Query( array(
                    \'post_type\'         => \'knowledge-base\', 
                    \'order\'      => \'ASC\',               
                    \'orderby\'           => \'meta_value_num\', 
                    \'meta_key\'          => \'top_four_num\',               
                    \'posts_per_page\'    => -4,                
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'topic\',
                            \'field\'    => \'slug\',
                            \'terms\'    => $term_slug,
                        ),
                    ),
                ));

        if( $_first->have_posts() ) :
            ?> <div class = "container1"><?php
     //Output Term name
            echo \'<div class = "term-name"><h2>\'. $term->name .\'</h2></div>\';
            echo \'<hr id="short-line" align="left" width="100">\';

            while ( $_first->have_posts() ) : $_first->the_post();
            ?>

     <?php 
             //Loop counter to add divs to first loop returned posts 
             if( 0 == $_first->current_post ) {
            ?> <div class = "example-left"> <?php
        }?>
     <?php  if( 2 == $_first->current_post ) {
    ?> </div> <?php
        }?>
       <?php  if( 2 == $_first->current_post ) {
            ?> <div class = "example-right"> <?php
        }?>         
             <?php // output the post infomation ?>
                <div class="answers">
                    <h4><a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                    <p><?php get_post_meta( get_the_ID(), \'url\', true ); ?></p>
                </div>    


            <?php
            endwhile;?>


    <?php // Begin Secondary Hidden Content loop ?>
               <?php $term_slug = $term->slug;
        $_second = new WP_Query( array(
                    \'post_type\'         => \'knowledge-base\',
                    \'order\'             => \'ASC\',
                    \'posts_per_page\'    => 10, //important for a PHP memory limit warning
                   /* \'offset\'            => 5*/
                    \'meta_key\'          => \'priority\',       
                    \'meta_value\'        => \'1\',  
                    \'meta_compare\'      => \'>=\',                 
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'topic\',
                            \'field\'    => \'slug\',
                            \'terms\'    => $term_slug,
                        ),
                    ),
                )); 

                if( $_second->have_posts() ) :
            ?>
    </div>
            <!---JQUERY HIDE SWITCH BUTTONS--> 
    <div class = "view-more"><a class="arrow-link" data-expand="howitworks-expand">View more<img class="main-arrow" src="/wp-content/uploads/[email protected]"></a></div>
    <div class = "show-content"><?php       
          $i = 0;
            while ( $_second->have_posts() ) : $_second->the_post();
            ?>


        <?php //Output hidden loop into two columns automatically
        if ($i == 0) echo \'<div class="one-half first">\';
      if ($i == (round($_second->post_count / 2))) echo \'</div><div class="one-half">\';   


       ?> <p class = "rest-of-p sub"><a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                    <p><?php get_post_meta( get_the_ID(), \'url\', true ); ?></p><?php


      if ($i == round($_second->post_count)) echo \'</div>\';
      $i++; 

    ?>        
          <?php endwhile;?>
     <?PHP wp_reset_postdata(); ?>
             </div>
                    <?php endif;?>

                   <?PHP //LOOP 2 END ?>
    <!--stop container-->
    </div>
    </div><?php       

        endif;
    endforeach;

    ?>
    </div>
    <?php
     // Call in footer 
    get_footer();

    ?>

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

get_terms documentation:

orderby (string): 订购条款的字段。接受术语字段(\'name\',\'slug\',\'term\\u group\',\'term\\u id\',\'id\',\'description\'),\'count\'用于术语分类计数,\'include\'用于匹配$include param的\'order\',\'meta\\u value\',\'meta\\u num\',\'meta\\u key的值,$meta\\u query的数组键,或\'none\'用于省略order BY子句。默认为“name”。

示例:

get_terms([
     \'taxonomy\' => \'topic\',
     \'orderby\'  => \'field\',
     \'order\'    => \'ASC/DESC\',    
]);