Last active
May 30, 2016 16:32
-
-
Save nicomollet/5a18896f56701e44b285 to your computer and use it in GitHub Desktop.
Jetpack Infinite Scroll: Posts ordered by meta value
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 | |
// Inspired by Isotrope code: https://gist.github.com/isotrope/1930f90518bdbfa074c3 and MCSF https://gist.github.com/mcsf/6c5cda9ee61acc8849c6 | |
// Creates a query var called "tri" | |
function query_vars_tri( $vars ){ | |
$vars[] = "tri"; | |
return $vars; | |
} | |
add_filter( 'query_vars', 'query_vars_tri' ); | |
// Posts ordered by queryvar "tri" | |
function posts_orderby_tri($query) { | |
if(!is_admin()){ | |
if($query->is_main_query() ){ | |
if(in_array($query->query_vars['tri'],array('general', 'pertinence', 'bienetre', 'protection', 'vieprivee', 'innovation', 'prixplus', 'prixmoins'))){ | |
$order = 'DESC'; | |
if($query->query_vars['tri']=='prixplus' || $query->query_vars['tri']=='prixmoins') | |
$meta_key = '_infos_prix'; | |
else | |
$meta_key = '_rating_'.$query->query_vars['tri']; | |
if($query->query_vars['tri']=='prixmoins') | |
$order = 'ASC'; | |
$orderby = 'meta_value_num'; | |
$query->set('orderby', $orderby); | |
$query->set('meta_key', $meta_key); | |
$query->set('order',$order); | |
} | |
} | |
} | |
} | |
add_action('pre_get_posts','posts_orderby_tri'); | |
// Whitelists the `extra` variable that holds the predicates, so that IS doesn't ignore it when reconstructing the original query using the query args from the IS AJAX request * request. | |
function infinite_scroll_allowed_vars_extra( $allowed_vars ) { | |
$allowed_vars[] = 'extra'; | |
return $allowed_vars; | |
} | |
add_filter( 'infinite_scroll_allowed_vars', 'infinite_scroll_allowed_vars_extra' ); | |
// Includes a few useful predicates in the bootstrapped `infiniteScroll` JS object that is served along with the rest of the document upon initial page request. | |
function infinite_scroll_js_settings_extra( $js_settings ) { | |
$js_settings['text'] = __( "Plus de contenus", "pla" ); | |
$js_settings['query_args']['extra'] = array( | |
'tri' => get_query_var( 'tri' ) , | |
); | |
return $js_settings; | |
} | |
add_filter( 'infinite_scroll_js_settings', 'infinite_scroll_js_settings_extra' ); | |
// Apply sorting methods to jetpacks queries using the "tri" query var | |
function infinite_scroll_query_args_tri( $query_args ) { | |
if(in_array($query_args['extra']['tri'],array('general', 'pertinence', 'bienetre', 'protection', 'vieprivee', 'innovation', 'prixplus', 'prixmoins'))){ | |
$order = 'DESC'; | |
if($query_args['extra']['tri']=='prixplus' || $query_args['extra']['tri']=='prixmoins') | |
$meta_key = '_infos_prix'; | |
else | |
$meta_key = '_rating_'.$query_args['extra']['tri']; | |
if($query_args['extra']['tri']=='prixmoins') | |
$order = 'ASC'; | |
$orderby = 'meta_value_num'; | |
$query_args['orderby'] = $orderby; | |
$query_args['meta_key'] = $meta_key; | |
$query_args['order'] = $order; | |
$query_args['offset'] = $query_args['posts_per_page'] * $query_args['paged']; | |
} | |
return $query_args; | |
} | |
add_filter( 'infinite_scroll_query_args', 'infinite_scroll_query_args_tri',11 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment