Created
July 30, 2015 21:03
-
-
Save opincus/f32f230cc26448ed8b27 to your computer and use it in GitHub Desktop.
facetWP
This file contains hidden or 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
// Added to archive-speakers.php | |
function my_facetwp_query_args( $query_args, $class ) { | |
$query_args['orderby'] = 'lastname'; | |
$query_args['meta_key'] = 'lastname'; | |
$query_args['order'] = 'asc'; | |
return $query_args; | |
} | |
add_filter( 'facetwp_query_args', 'my_facetwp_query_args', 10, 2 ); | |
// Added this to functions.php to make sure CPTs are sorted when entering the archive page | |
function speaker_random_query( $query ) { | |
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && $query->is_post_type_archive( 'speaker' ) ) { | |
//$query->set( 'orderby', 'rand' ); | |
//CPTs are randomized, but not paginated correctly | |
$query->set( 'orderby', 'lastname' ); | |
$query->set( 'meta_key', 'lastname'); | |
$query->set( 'order', 'ASC' ); | |
$query->set( 'posts_per_page', '8' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'speaker_random_query' ); | |
// Added this to functions.php, but pagination did not work on pages 1+ | |
//session_start(); | |
function randomise_with_pagination( $orderby, $wp_query ) { | |
if ( 'rand' == $wp_query->query_vars['orderby'] ) { | |
// Reset seed on load of initial archive page | |
//echo 'xxxxxxxxxxxxxxxxx'; | |
if( ! get_query_var( 'paged' ) || get_query_var( 'paged' ) == 0 || get_query_var( 'paged' ) == 1 ) { | |
if( isset( $_SESSION['seed'] ) ) { | |
unset( $_SESSION['seed'] ); | |
} | |
} | |
// Get seed from session variable if it exists | |
$seed = isset( $_SESSION['seed'] ) ? $_SESSION['seed'] : rand(); | |
// Set new seed if none exists | |
if ( ! $seed ) { | |
$seed = rand(); | |
$_SESSION['seed'] = $seed; | |
} | |
// Update ORDER BY clause to use seed | |
$orderby = 'RAND(' . $seed . ')'; | |
} | |
return $orderby; | |
} | |
add_filter( 'posts_orderby', 'randomise_with_pagination', 10, 2 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment