[wp-hackers] sortable taxonomy columns for taxonomy metadata

Roberto Sanchez roberto at digitalbrands.com
Tue Jan 15 20:38:33 UTC 2013


A few more details I didn't add in my last reply:

The first Scribu post dealing with sorting columns helped with sorting
pages on a custom post type I had. However, I am now trying to sort on a
custom taxonomy for that custom post type. One other difference is that the
custom post type uses /wp-admin/edit.php, while the custom taxonomy uses
/wp-admin/edit-tags.php. As such, the filters in the first Scribu post
don't work for the custom taxonomy. The page edit.php uses
WP_Posts_List_Table, while edit-tags.php uses WP_Terms_List_Table. I've
been trying to dig in there to find which filters are used differently
between the two list tables but haven't had much luck with that.

Starting from the second post I found on Scribu, I came up with this:

    add_filter( 'posts_clauses', 'sort_by_pageviews', 10, 2 );

   function sort_by_pageviews( $clauses, $wp_query ) {
       global $wpdb;

        if ( isset( $_GET['orderby'] ) && 'pageviews' == $_GET['orderby'] )
{
            $clauses['join'] .= "LEFT JOIN (
                SELECT object_id, GROUP_CONCAT(meta_value ORDER BY
meta_value ASC) AS pageviews
                FROM $wpdb->term_relationships
                INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id)
                INNER JOIN $wpdb->taxonomymeta ON
($wpdb->taxonomymeta.taxonomy_id =
$wpdb->term_relationships.term_taxonomy_id)
                WHERE taxonomy = 'merchant'
                AND $wpdb->taxonomymeta.meta_key = 'pageviews'
                GROUP BY object_id
            ) AS pageviews_terms ON ( $wpdb->posts.ID =
pageviews_terms.object_id)";
            $clauses['orderby'] = "pageviews_terms.pageviews ";
            $clauses['orderby'] .= ( isset( $_GET['order'] ) && 'ASC' ==
strtoupper( $_GET['order'] ) ) ? 'ASC' : 'DESC';
        }

        return $clauses;
    }

A couple of things here:

I'm not sure if I'm even using the correct filter here, since
$wp_query->query['orderby'] is not being set. This is why I needed to check
the $_GET variables instead. I don't understand why the query doesn't use
the variables passed by $_GET.

Additionally, I'm using the Taxonomy Metadata plugin:

http://wordpress.org/extend/plugins/taxonomy-metadata/

I'm pretty sure this plugin adds the wp_taxonomymeta table to the DB, but
the table name is still accessible via $wpdb->taxonomymeta.

This method and the previous method don't work for sorting the taxonomy by
the pageviews metadata.


More information about the wp-hackers mailing list