WordPress Plugin Development: How can I make a sortable custom column of a custom post type listing table by custom expression?
-
I'm trying to create a custom sortable column in a post listing table of a custom post type. The problem is that each column cell value is an expression of multiple meta values. I could display the calculated value in each cell but I could not find a way to sort the column. Say, each post of my custom post type has two meta keys, 'followers' and 'mentions'. I've created a column that displays the number of followers divided by the number of mentions. How can I make it sortable? (I could make it sortable but it does not sort it properly) It seems to be achieved by modifying the MySQL query with the post_clauses hook as introduced in the below articles. http://scribu.net/wordpress/sortable-taxonomy-columns.html http://wordpress.stackexchange.com/questions/109955/custom-table-column-sortable-by-taxonomy-query However, they are for taxonomy columns and not exactly what I'm looking for. I'm guessing that the part dealing with the 'orderby' array element needs to be adjusted for my needs but not sure how. $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) "; $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
-
Answer:
Okay this worked. if ( is_admin() && 'edit.php' == $GLOBALS['pagenow'] && isset( $_GET['post_type'] ) && 'my_post_type' == $_GET['post_type'] && isset( $_GET['orderby'], $_GET['order'] ) ) { add_filter( 'posts_clauses', 'modify_query_clauses', 10, 2 ); } function modify_query_clauses( $clauses, $query ) { global $wpdb; if ( isset( $query->query['orderby'] ) && 'custom_expression' == $query->query['orderby'] ) { $clauses['join'] .= "INNER JOIN {$wpdb->postmeta} AS m1 on {$wpdb->posts}.ID = m1.post_id "; $clauses['join'] .= "INNER JOIN {$wpdb->postmeta} AS m2 on {$wpdb->posts}.ID = m2.post_id"; $clauses['where'] .= " AND m1.meta_key = 'followers'"; $clauses['where'] .= " AND m2.meta_key = 'mentions'"; $clauses['orderby'] = "( m1.meta_value / m2.meta_value ) "; $clauses['orderby'] .= ( 'ASC' == strtoupper( $query->get('order') ) ) ? 'ASC' : 'DESC'; } return $clauses; }
Anonymous at Quora Visit the source
Related Q & A:
- How can I make a myspace comment into a picture caption?Best solution by Yahoo! Answers
- How can I make a hole through a stone or crystal?Best solution by Yahoo! Answers
- How can I make a phone call to a cruise ship?Best solution by traveltips.usatoday.com
- How can i make a banner for a website?
- How can I make a good clay for a school project?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.