2

I have created a custom taxonomy called 'attach_cats' for media attachments. Each term in the taxonomy has custom term metadata with a key of 'term_order' and meta value of an integer. I have added a custom column to the "edit-tag.php" page that displays the value of 'term_order'.

Now I would like to order the terms to 'orderby' the value of 'term_order'. I do this all the time on the "edit.php?post_type=page" with the following function:

add_action('pre_get_posts', 'sort_by_meta_key');
function sort_by_meta_key($query){
    global $pagenow;
    if(is_admin()
        && 'edit.php' == $pagenow
        && isset($_GET['post_type'])  && $_GET['post_type']=='gallery'){
        $query->set( 'meta_key', 'gallery_pos' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'ASC' );
    }
}

But, when I try to do it with "terms" instead of "posts" using "pre_get_terms" it doesn't work. Here is what I have:

add_action('pre_get_terms', 'term_sort_order');
function term_sort_order($query){
    global $pagenow;
    if(is_admin()
        && 'edit-tags.php' == $pagenow
        && isset($_GET['post_type'] )  && $_GET['post_type']=='attachment' ){
        $query->set( 'meta_key', 'term_order' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'ASC' );
    }
}

// THIS RETURNS: Fatal error: Call to undefined method WP_Term_Query::set() ......

Any help would be greatly appreciated.

Jeremy
  • 239
  • 2
  • 14

2 Answers2

1

try 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';
}
  • You gotta have an action for pre_get_terms and then in the callback function you try that above. worked for me. the solution came in the comments in this link: https://core.trac.wordpress.org/ticket/34996 the comment of eherman24 – Lucas Gabriel Nov 10 '17 at 14:00
  • Thanks for the reply, even though it has been 6 months, I haven't done anything else on that project since then (it was a personal plugin I was building).... I will get back to it tomorrow and try this... thank you. – Jeremy Nov 11 '17 at 15:49
0

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

just dont forget to add the action hook pre_get_terms and then use the callback provided above.