Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
combination of parts needed to add a new parameter on the wordpress url for sorting.
<?php
//adds the orderby parameter to the url for the sorting
function add_orderby() {
if ( isset( $_GET['orderby'] ) ) {
if ( in_array( $_GET['orderby'],[]) ) {
set_query_var( 'orderby', 'meta_value_num' );
set_query_var( 'meta_key', $_GET['orderby'] );
}
}
}
add_filter( 'pre_get_posts', 'add_orderby' );
//gets url parameters
function GetURLParameter(sParam) {
var sPageURL = window.location.search.substring ( 1 );
var sURLVariables = sPageURL.split ( '&' );
for ( var i = 0 ; i < sURLVariables.length ; i++ ) {
var sParameterName = sURLVariables[ i ].split ( '=' );
if ( sParameterName[ 0 ] == sParam ) {
return sParameterName[ 1 ];
}
}
}
//creates the right url
$('#sortbox').change(function(){
var search = GetURLParameter('s');
var category = GetURLParameter('cat');
var location = '?s='+search+'&cat='+category+'&orderby=date&order='+this.options[this.selectedIndex].value;
window.location.replace(location);
});
//shows the right option on the selector
$('#sortbox').val(GetURLParameter('order'));
<h3>Sort by</h3>
<form>
<select class="dropdown-class" name="sort-posts" id="sortbox">
<option disabled>Sort by</option>
<option value="">Relevance</option>
<option value="DESC">Newest</option>
<option value="ASC">Oldest</option>
</select>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.