• I have a custom post type (and related custom taxonomy). In the custom post type’s Admin screen, I have various meta fields shown in columns and some of them sortable.

    One of the columns is “Sequence” which displays the _ejp_seq meta value, which is intended to be a numeric value. I also have a selection drop down for the custom taxonomy and it’s these two things which appear to clash.

    I have the following code to allow the sort to work:

    function sequence_column_orderby( $vars ) {
        if ( isset( $vars['orderby'] ) && 'sequence' == $vars['orderby'] ) {
            $vars = array_merge( $vars, array(
                'meta_key' => '_ejp_seq',
                'orderby' => 'meta_value_num'
            ) );
        }
        echo "%%%%%"; var_dump($vars);
        return $vars;
    }
    
    add_filter( 'request', 'sequence_column_orderby' );

    Notice that var_dump to show me what’s going on. If I show all of the custom posts and sort by sequence, the $vars array ends up as:

    ["m"]=> string(1) "0"
    ["s"]=> string(0) ""
    ["paged"]=> string(1) "1"
    ["order"]=> string(3) "asc"
    ["orderby"]=> string(14) "meta_value_num"
    ["post_type"]=> string(14) "ej_prod"
    ["collection"]=> string(1) "0"
    ["posts_per_page"]=> int(20)
    ["meta_key"]=> string(8) "_ejp_seq"

    Things to note are the orderby and meta_key values and collection, which is my custom taxonomy, has 0 to show all posts.

    In this case, my 11 posts are sorted correctly by sequence 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. The 2nd of the 1’s is a different collection to the rest.

    Now if I use my dropdown to select only the collection that contains the ten sequential sequence numbers (so dropping that 2nd 1 above), the $vars array looks like this:

    ["m"]=> string(1) "0"
    ["s"]=> string(0) ""
    ["paged"]=> string(1) "1"
    ["order"]=> string(3) "asc"
    ["orderby"]=> string(14) "meta_value_num"
    ["post_type"]=> string(14) "ej_prod"
    ["collection"]=> string(1) "4"
    ["posts_per_page"]=> int(20)
    ["meta_key"]=> string(8) "_ejp_seq"

    Note the only difference is collection is now set to 4. But the 10 posts now sort as 1, 10, 2, 3, 4, 5, 6, 7, 8, 9.

    I’ve also noted that in the filtered scenario, if I click the column heading again to switch to descending order, order changes to desc but the actual sort order is identical. When unfiltered, the descending order is shown correctly.

    So it appears for some reason when I filter my post list, the sorting is being ignored, or at least corrupted somehow.

    Here’s the code to add the dropdown for the filter. This is the least understood chunk of code I’ve used in all this work, but it appears to fundamentally work for me. I tried removing that orderby line but it made no difference. I’m sure it’s something in here that’s causing the problem but am at a loss.

    if ( in_array( $typenow, $post_types ) ) {
            $filters = get_object_taxonomies( $typenow );
    
            foreach ( $filters as $tax_slug ) {
                $tax_obj = get_taxonomy( $tax_slug );
                wp_dropdown_categories( array(
                    'show_option_all' => __('Show All '.$tax_obj->label ),
                    'taxonomy'    => $tax_slug,
                    'name'        => $tax_obj->name,
                    'orderby'     => 'name',
                    'selected'    => $_GET[$tax_slug],
                    'hierarchical'    => $tax_obj->hierarchical,
                    'show_count'      => false,
                    'hide_empty'      => true
                ) );
            }
        }
    }
    
    add_action( 'restrict_manage_posts', 'taxonomy_filter_restrict_manage_posts' );
    
    function taxonomy_filter_post_type_request( $query ) {
        global $pagenow, $typenow;
    
        if ( 'edit.php' == $pagenow ) {
            $filters = get_object_taxonomies( $typenow );
            foreach ( $filters as $tax_slug ) {
                $var = &$query->query_vars[$tax_slug];
                if ( isset( $var ) ) {
                    $term = get_term_by( 'id', $var, $tax_slug );
                    $var = $term->slug;
                }
            }
        }
    }
    
    add_filter( 'parse_query', 'taxonomy_filter_post_type_request' );

    Any help or pointers appreciated.

  • The topic ‘Sorting by meta_value_num in Admin not working when filtered by custom taxonomy’ is closed to new replies.