Last active
October 29, 2017 14:05
-
-
Save techslides/69ebdc66cdc8bc397f99e683d571179e to your computer and use it in GitHub Desktop.
Sort WordPress Posts by Modified Date using Query Param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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