Skip to content

Instantly share code, notes, and snippets.

@techslides
Last active October 29, 2017 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techslides/69ebdc66cdc8bc397f99e683d571179e to your computer and use it in GitHub Desktop.
Save techslides/69ebdc66cdc8bc397f99e683d571179e to your computer and use it in GitHub Desktop.
Sort WordPress Posts by Modified Date using Query Param
<?php
//put this into functions.php and add url param to homepage: sortby=updated
function custom_query_vars_filter($vars) {
$vars[] = 'sortby';
//$vars[] .= 'another';
return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );
function techslides_alter_query($query){
$sortby = get_query_var( 'sortby' );
if( $sortby=="modified" && !is_admin() && $query->is_main_query() ){
$query->set( 'orderby', 'modified' );
}
}
add_action('pre_get_posts','techslides_alter_query');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment