Skip to content

Instantly share code, notes, and snippets.

@jakewhiteley
Last active August 29, 2015 14:09
Show Gist options
  • Save jakewhiteley/0d133f30688e0adff27e to your computer and use it in GitHub Desktop.
Save jakewhiteley/0d133f30688e0adff27e to your computer and use it in GitHub Desktop.
<?php
/**
* instruct wordpress what orderby=template means
* @param Array wordpress request rules
* @return Array modified $vars
*/
function template_column_orderby( $vars ) {
// the search extras that will order correctly
if ( isset( $vars['orderby'] ) && 'template' == $vars['orderby'] ) {
$vars = array_merge( $vars, array( // merge the existing search parameters with ours
'meta_key' => '_wp_page_template', // this is the meta vale to order by
'orderby' => 'meta_value title', // order by the meta_key we define, then by title
/*
* ordering the pages by a meta key, will only return pages with a value present.
* to get by this, we tell Wordpress to include pages with or without the value
*/
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_wp_page_template',
'compare' => 'NOT EXISTS'
),
array(
'key' => '_wp_page_template',
'compare' => 'EXISTS'
),
)
) );
}
return $vars;
}
add_filter('request', 'template_column_orderby');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment