Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • Programming
    • C
    • C++
    • Java
  • Web Development
    • Java Script
    • jQuery
    • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • Programming
    • C
    • C++
    • Java
  • Web Development
    • Java Script
    • jQuery
    • PHP

Custom taxonomy with custom meta value is not sorting correctly (query returns the same value for orderby regardless of sort column click)

pre_get_posts is an action hook used for modifying the arguments passed to the WP_Query class, and if you’re filtering the posts in the list table at wp-admin/edit.php, then yes, you would use that hook.

However, since you’re filtering the terms in the list table at wp-admin/edit-tags.php, then the hook that you should have used is pre_get_terms, which can be used to modify the arguments passed to the WP_Term_Query class.

  • That class is used by get_terms() which is used by the class which renders the terms list table.

But then, I don’t know for sure why ( maybe it’s a bug in WordPress core? 🤔 ), but when sorting by meta value, i.e. when orderby is meta_value_num, meta_value, or a key in a meta_query array, it seems we need to use the parse_term_query hook instead, which runs before pre_get_terms.

And here’s an example you can try, and note that unlike WP_Query, WP_Term_Query doesn’t have a get() or set() method, hence I directly accessed/modified the query_vars property:

add_action( 'parse_term_query', 'weight_column_orderby' );
function weight_column_orderby( WP_Term_Query $query ) {
    // Check whether we are at wp-admin/edit-tags.php?taxonomy=weight-class
    if ( ! is_admin() ||
        ! function_exists( 'get_current_screen' ) ||
        'edit-weight-class' !== get_current_screen()->id
    ) {
        return;
    }

    $taxonomies = (array) $query->query_vars['taxonomy'];

    // Modify the args, if `weight` is the `orderby` value, and that the query
    // is for your custom `weight-class` taxonomy.
    if ( 'weight' === $query->query_vars['orderby'] &&
        in_array( 'weight-class', $taxonomies, true )
    ) {
        $query->query_vars['meta_key'] = 'weight';
        $query->query_vars['orderby']  = 'meta_value_num';
    }
}

Categories custom-taxonomy Tags custom-taxonomy, meta-query, post-meta, pre-get-posts, sort
Where can I find the definitive list of core blocks in html form
Dealing with unknown option ‘–variant’ when setting up a block environment

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • register_nav_menus() not registering
  • WordPress is updating only patch version and not minor
  • Prefill the textarea in comment_form
  • Is it smart to require_once wp-admin/includes/plugin.php when you need only one function from it
  • Virus injected to my wordpress site
  • WooCommerce, change “Add to Cart” to “Link to Product”, only for specific categories
  • How to add embed image in comments?
  • WPDB query suddenly not working
  • How to quickly search posts by title in Admin interface
  • WordPress Block Editor Gutenberg running code inside ! is_admin()
© 2023 Read For Learn
  • Programming
    • C
    • C++
    • Java
    • javascript
    • Python
    • R
  • Web Development
    • Java Script
    • jQuery
    • PHP
    • WordPress
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Microsoft Office
  • modx
  • root
  • Tips & Trick
  • Tools
  • windows