0

I am trying to work out how I can define the order of terms in my WP_QUERY.

I am creating a knowledge base. I have a CPT called knowledge-base and a taxonomy called topics.

My code outputs all of the topic terms and then loops through 4 posts that have the meta key top_four_num, this is to show the top four questions in the topic. Then I have a nested loop that the rest of the posts in that topic. These are hidden by JQUERY, the user clicks a 'show all link' to reveal.

Everything is working as I want it to, but I cannot work out how to change to order of the terms. Currently, they appear in Alphabetical order.

I have checked to codex and tax_query has no order based parameters. Can anyone point me in the right direction to being able to control tax_query order?

My code is below. Any help is greatly appreciated!

        <?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/arrow@2x-1.png"></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();

    ?>
2

From the get_terms documentation:

orderby (string): Field(s) to order terms by. Accepts term fields ('name', 'slug', 'term_group', 'term_id', 'id', 'description'), 'count' for term taxonomy count, 'include' to match the 'order' of the $include param, 'meta_value', 'meta_value_num', the value of $meta_key, the array keys of $meta_query, or 'none' to omit the ORDER BY clause. Defaults to 'name'.

Example:

get_terms([
     'taxonomy' => 'topic',
     'orderby'  => 'field',
     'order'    => 'ASC/DESC',    
]);
  • Great!! Works perfectly, completely missed the get_terms array. Thanks again! :) – tmgale12 Jun 29 '16 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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