Skip to content

Instantly share code, notes, and snippets.

@lstanard
Last active March 1, 2016 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lstanard/34d9d603106334b9fdf6 to your computer and use it in GitHub Desktop.
Save lstanard/34d9d603106334b9fdf6 to your computer and use it in GitHub Desktop.
JavaScript select menu on change for WordPress post filtering
$('select[name="post_date"]').on('change', function() {
var postDate = this.options[this.selectedIndex].value,
dateRE = /\d{4}[-]\d{1,2}|\d{4}/g,
prevDate = dateRE.exec(url);
// Drop pagination when changing the post_date filter value
if (url.indexOf('page/') > 0)
url = url.split('page/')[0];
// If a date exists in URL already, drop it and go to new date,
// otherwise go directly to the date specified
window.location = (prevDate) ? url.split(prevDate[0])[0] + postDate + '/' : url + postDate + '/';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment