1

Slowly going crazy over trying to make taxonomy admin columns sortable by a custom field number. I have a custom taxonomy called "typer" and these taxonomies have a custom field called "prioritet".

I managed to get my code to show the column, and make it sortable. Only issue is, that the fields are sorted alphabetically, despite all being numbers. thats means that a fieldset of [1, 3, 14, 22] would show as:

1

14

22

3

My code so far

function create_date_column_for_issues($issue_columns) {
  $issue_columns['prioritet'] = 'Prioritet / sortering';
  return $issue_columns;
}
add_filter('manage_edit-typer_columns', 'create_date_column_for_issues');

function populate_date_column_for_issues($value, $column_name, $term_id) {
  $issue = get_term($term_id, 'typer');
  $date = get_field('prioritet', $issue);
  switch($column_name) {
    case 'prioritet': 
        $value = $date;  


    break;
    default:

    break;
  }
  return $value;    
}
add_filter('manage_typer_custom_column', 'populate_date_column_for_issues', 10, 3);

function register_date_column_for_issues_sortable($columns) {
  $columns['prioritet'] = 'prioritet';
  return $columns;
}
add_filter('manage_edit-typer_sortable_columns', 'register_date_column_for_issues_sortable');

add_filter( 'terms_clauses', 'filter_terms_clauses', 10, 3 );

/**
 * Filter WP_Term_Query meta query
 *
 * @param   object  $query  WP_Term_Query
 * @return  object
 */
function filter_terms_clauses( $pieces, $taxonomies, $args ) {

    global $pagenow, $wpdb; 

    // Require ordering
    $orderby = ( isset( $_GET['orderby'] ) ) ? trim( sanitize_text_field( $_GET['orderby'] ) ) : ''; 
    if ( empty( $orderby ) ) { return $pieces; }

    // set taxonomy
    $taxonomy = $taxonomies[0];

    // only if current taxonomy or edit page in admin           
    if ( !is_admin() || $pagenow !== 'edit-tags.php' || !in_array( $taxonomy, [ 'typer' ] ) ) { return $pieces; }

    // and ordering matches
    if ( $orderby === 'prioritet' ) {
        $pieces['join']  .= ' INNER JOIN ' . $wpdb->termmeta . ' AS tm ON t.term_id = tm.term_id ';
        $pieces['where'] .= ' AND tm.meta_key = "prioritet"'; 
        $pieces['orderby']  = ' ORDER BY tm.meta_value '; 
    }

    return $pieces;
}

My knowledge on MySQL is next to nothing, so i have no idea where to go width this.

As an extra:

Right now the sorting will exclude all taxonomies that has an empty field. Would be nice to have these shown in the bottom, but i do realize i requires a way more complicated query. So only join in on that if just so happen to have the solution on the top of your head.

2

The code I am posting is a modified and simplified version of yours. I got my solution using your code.

/**
 * Filter WP_Term_Query meta query
 *
 * @param   object  $query  WP_Term_Query
 * @return  object
 */
function filter_terms_clauses( $pieces, $taxonomies, $args ) {

    global $pagenow, $wpdb;

    if(!is_admin()) {
        return $pieces;
    }

    if(
        is_admin() 
        && $pagenow == 'edit-tags.php' 
        && $taxonomies[0] == 'typer' 
        && ( isset($_GET['orderby']) && $_GET['orderby'] == 'prioritet' )
    ) {

        $pieces['join']     .= ' INNER JOIN ' . $wpdb->termmeta . ' AS tm ON t.term_id = tm.term_id ';
        $pieces['where']    .= ' AND tm.meta_key = "prioritet"'; 
        $pieces['orderby']   = ' ORDER BY tm.meta_value ';
        $pieces['order']     = isset($_GET['order']) ? $_GET['order'] : "DESC";
    }

    return $pieces;
}

add_filter( 'terms_clauses', 'filter_terms_clauses', 10, 3 );

Hope this one helps.

1

for me, I made a custom taxonomy and in that custom taxonomy I had a custom meta. I wanted to have in the admin backend a column and made it sortable. to make sortable work for a custom taxonomy in a custom meta I did this.

https://pastebin.com/vr2sCKzX

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';
}

I found the answer in this link https://core.trac.wordpress.org/ticket/34996

I just had to adapt the answer provided in the comments by @eherman24

1
  • dont forget that the function pre_get_terms is called as a callback for the action hook pre_get_terms – Lucas Gabriel Nov 10 '17 at 14:23

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.