Created
July 20, 2012 16:25
-
-
Save tpennock-gist/3151675 to your computer and use it in GitHub Desktop.
Wordpress Post Sorting for 'popular' and 'newest', captures post view count and sets it in the DB for each post (popularity factor), captures query string parameters for minor class replacements of the filter buttons (for css)
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
/* | |
Function: getPostViews (custom) | |
Gets the Post Views count | |
Returns: | |
$count.' Views' | |
*/ | |
function getPostViews($postID){ | |
$count_key = 'post_views_count'; | |
$count = get_post_meta($postID, $count_key, true); | |
if($count==''){ | |
delete_post_meta($postID, $count_key); | |
add_post_meta($postID, $count_key, '0'); | |
return "0 View"; | |
} | |
return $count.' Views'; | |
} | |
/* | |
Function: setPostViews (custom) | |
Sets the Post Views count | |
*/ | |
function setPostViews($postID) { | |
$count_key = 'post_views_count'; | |
$count = get_post_meta($postID, $count_key, true); | |
if($count==''){ | |
$count = 0; | |
delete_post_meta($postID, $count_key); | |
add_post_meta($postID, $count_key, '0'); | |
}else{ | |
$count++; | |
update_post_meta($postID, $count_key, $count); | |
} | |
} |
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
// get querystring as an array split on "&" | |
var querystring = location.search.replace( '?', '' ).split( '&' ); | |
// declare object | |
var queryObj = {}; | |
// loop through each name-value pair and populate object | |
for ( var i=0; i<querystring.length; i++ ) { | |
// get name and value | |
var name = querystring[i].split('=')[0]; | |
var value = querystring[i].split('=')[1]; | |
// populate object | |
queryObj[name] = value; | |
} | |
// Check query string for sort filter method | |
if(queryObj[ "filter" ] === "popular"){ | |
$('#newest').removeClass('active'); | |
$('#popular').addClass('active'); | |
} else if(queryObj[ "filter" ] === "newest"){ | |
$('#popular').removeClass('active'); | |
$('#newest').addClass('active'); | |
} |
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
<? | |
// Place this code into your archive.php template file | |
$filter = $_GET['filter']; | |
// Preserve existing query parameters | |
global $wp_query; | |
// Check query string parameter "filter" (options: 'popular' and 'newest') | |
if((isset($filter)) && $filter == 'popular'){ | |
$args = array_merge( $wp_query->query, array('order' => 'desc', 'orderby' => 'meta_value_num', 'meta_key' => 'post_views_count') ); | |
query_posts( $args ); | |
}else if((isset($filter)) && $filter == 'newest'){ | |
$args = array_merge( $wp_query->query, array('order' => 'desc') ); | |
query_posts( $args ); | |
} | |
?> |
Author
tpennock-gist
commented
Jul 20, 2012
- functions.php - Add to your theme's functions.php file, Counts the total views for each post and adds it to DB under the _sitemeta table with meta_key = post_views_count. Returns post_views_count value.
- template.js - Add to your theme's template.js file, Grabs the query string parameter selected and creates queryObj to be used for other logic.
- Wordpress Post Sorting - Checks the query string set and performs the appropriate query_posts() call with $args, while preserving existing query parameters so we don't kill the default query loop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment