1

I want to make my custom terms to be sortable by users. I've created different links to let the user decide how they want to sort it.

<a href="?orderby=name&order=asc" data-target="#">Name ascending</a>
<a href="?orderby=name&order=desc" data-target="#">Name descending</a>

<?php
$orderby = get_query_var('orderby', 'name');
$order = get_query_var('order', 'ASC');

echo $orderby . " : " . $order;  
$args = [
    'number' => $tpp, // Amount of terms to return
    'offset' => $offset, // The amount to offset the list by for pagination
    'orderby' => $orderby,
    'order' => $order
 ];
 $actors = get_terms( 'myTaxonomy', $args );

It works properly if the query vars are set. But if no query var is provided, I always get DESC.

As the default value for $order although I defined ASC as default.
What am I missing?

1

The default value of get_query_var( $var, $default ) is only returned if the query variable $var isn't available in the global $wp_query object.

The order query variable actually falls back to the DESC value here:

if ( ! isset( $q['order'] ) ) {
    $q['order'] = $rand ? '' : 'DESC';
} ...

within WP_Query::get_posts(), so that's why it's used instead of your default value.

Be careful when applying native public query variables, to secondary queries, because they can also change the main query result.

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.