This is my first time posting a question here.
I have this problems that I feel that I am soooo close to solve, but can’t manage to do it.
I have a custom taxonomy called "aplication" and another called "segment". Each "aplication" term has an "segment" term associated with it trought a custom field (ACF).
What I’m trying to do is to sort the "aplication" terms alphabetically by their associated "segment" term in the edit-tag?taxonomy=aplication page.
I managed to add the sortable "segment" column into the page through the code below:
// Add the "segment" column to the list of "aplication" terms add_filter('manage_edit-aplication_columns', function( $columns ) { $columns['aplication_segment'] = __( 'Segment', 'textdomain' ); return $columns; }); // Add data to the "segment" column created above add_action( 'manage_aplication_custom_column', function( $value, $column, $aplication_id ) { if ( $column == 'aplication_segment') { $segment_ID = get_field( 'aplication_segment', get_term($aplication_id, 'aplication') ); // The custom field for the aplications returns the segment ID $segment = get_term( $segment_ID, 'segment' ); $value = $segment -> name; echo $segment->name; } }, 10, 3); // Make the "segment" column sortable add_filter('manage_edit-aplication_sortable_columns', function( $columns ) { $columns['aplication_segment'] = 'aplication_segment'; return $columns; });
Now, to sort the "aplication" terms I did this:
add_action('pre_get_terms', function( $term_query ) { global $current_screen; global $wpdb; if ( ($current_screen) && $current_screen->id === 'edit-aplication' ) { if ( $term_query -> query_vars['orderby'] === 'aplication_segment' ) { $sql = "SELECT ttm.term_id, ttm.name, ttm.slug, ttm.term_group, ttm.term_order FROM (SELECT wp_terms.term_id, wp_terms.name, wp_term_taxonomy.taxonomy FROM wp_terms INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id AND wp_term_taxonomy.taxonomy = 'segment' ORDER BY wp_terms.name) as ttt INNER JOIN (SELECT wp_terms.*, wp_termmeta.meta_value FROM wp_terms INNER JOIN wp_termmeta ON wp_terms.term_id = wp_termmeta.term_id AND wp_termmeta.meta_key = 'aplication_segment' ORDER BY wp_terms.name) as ttm ON ttt.term_id = ttm.meta_value ORDER BY ttt.name {$term_query->query_vars['order']}"; return $wpdb -> get_results( $sql ); }; } }, 10, 1);
This query works, it gives me the result that I need (I didn’t translated those to english):
Array ( [0] => stdClass Object ( [term_id] => 147 [name] => Puxadores [slug] => puxadores-moveleiros [term_group] => 0 [term_order] => 0 ) [1] => stdClass Object ( [term_id] => 3391 [name] => Cantoneiras [slug] => cantoneiras [term_group] => 0 [term_order] => 0 ) [2] => stdClass Object ( [term_id] => 150 [name] => Automotivo [slug] => automotivo [term_group] => 0 [term_order] => 0 ) [3] => stdClass Object ( [term_id] => 149 [name] => Dissipadores [slug] => dissipadores [term_group] => 0 [term_order] => 0 ) [4] => stdClass Object ( [term_id] => 3393 [name] => Luminárias [slug] => luminarias [term_group] => 0 [term_order] => 0 ) [5] => stdClass Object ( [term_id] => 148 [name] => Base Divisória [slug] => base-divisoria [term_group] => 0 [term_order] => 0 ) [6] => stdClass Object ( [term_id] => 3392 [name] => Esquadrias [slug] => esquadrias [term_group] => 0 [term_order] => 0 ) )
The only problem is that I can’t manage to show the result in the "aplication" terms table. It keeps sorting by the "aplication" term name.
I appreciate anyone who can try and help me with this.