0

I created a custom taxonomy using following code.

add_action( 'init', 'schedule_day_tax_func' );
function schedule_day_tax_func() {
    register_taxonomy(
        'schedule_day_taxonomy',
        'schedule',
        array(
            'hierarchical' => true,
            'label' => 'Day',
            'query_var' => true,
            'orderby'  => 'ID', 
        )
    );
}

after I added some terms from the admin panel, the TERMS displayed in alphabetical order in admin panel.

img

I want those in orderby ID or by the entered order. How can I do that?

1

Method #1 - Modifying the query arguments

If we want to modify the term order for the schedule_day_taxonomy term checklist, on the single schedule cpt edit screen, then we can use the following setup:

add_action( 'load-post.php', function()
{
    add_filter( 'wp_terms_checklist_args', 'wpse_terms_checklist_args' );
} );

function wpse_terms_checklist_args( $args )
{         
    // Target the 'schedule' custom post type edit screen
    if( 'schedule' === get_current_screen()->id )
        add_filter( 'get_terms_args', 'wpse_terms_args', 10, 2 );
    return $args;
}

function wpse_terms_args( $args, $taxonomies )
{
    // Target the 'all' tab in the 'schedule_day_taxonomy' terms check list 
    if(
           isset( $args['get'] )
        && 'all' === $args['get']
        &&  isset( $taxonomies[0] )
        && 'schedule_day_taxonomy' === $taxonomies[0]
    ) {
        // Modify the term order
        $args['orderby'] = 'ID';  // <-- Adjust this to your needs!
        $args['order']   = 'ASC'; // <-- Adjust this to your needs!

        // House cleaning - Remove the filter callbacks
        remove_filter( current_filter(), __FUNCTION__ );        
        remove_filter( 'wp_terms_checklist_args', 'wpse_terms_checklist_args' );
    }
    return $args;
}

Another possibility would be to combine this into a single get_terms_args filter callback.

To get the following day order:

Sun, Mon, Tue, Wed, Thu, Fri, Sat

we could modify the corresponding term slugs and use:

// Modify the term order
$args['orderby'] = 'slug';  
$args['order']   = 'ASC'; 

Method #2 - Re-ordering the query results

If we don't want to modify the slug, then here's another approach.

We can re-order the resulting terms array, in the same order as the localized weekdays.

add_filter( 'wp_terms_checklist_args', function( $args )
{         
    add_filter( 'get_terms', 'wpse_get_terms', 10, 3 );
    return $args;
} );

function wpse_get_terms( $terms, $taxonomies, $args )
{
    if(     isset( $taxonomies[0] )
        && 'schedule_day_taxonomy' === $taxonomies[0]
        && 'schedule' === get_current_screen()->id
    ) {
        $newterms = [];
        foreach( (array) $GLOBALS['wp_locale']->weekday as $day )
        {
            foreach( (array) $terms as $term )
            {
                if( $term->name === $day )
                    $newterms[] = $term;
            }
        }
        remove_filter( current_filter(), __FUNCTION__ );
        if( 7 === count( $newterms ) )
            $terms = $newterms;
    }
    return $terms;
}

We could also replace

$GLOBALS['wp_locale']->weekday

with a custom weekdays array, ordered in a custom way.

Here's the English version:

English

and here's the Icelandic version:

Icelandic

Method #3 - Custom SQL ordering

We could modify the SQL ordering with the get_terms_orderby filter, with a CASE/WHEN/THEN setup.

Hopefully you can adjust this to your needs.

0

I found the solution at this link: https://core.trac.wordpress.org/ticket/34996

public function pre_get_terms( $query ) {
  $meta_query_args = array(
    'relation' => 'AND', // Optional, defaults to "AND"
    array(
        'key'     => 'order_index',
        'value'   => 0,
        'compare' => '>='
    )
  );
  $meta_query = new WP_Meta_Query( $meta_query_args );
  $query->meta_query = $meta_query;
  $query->orderby = 'position_clause';
}

The answer was in the comment of @eherman24. all you need to adapt is adding an action pre_get_terms and then use the above function.

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.