Skip to content

Instantly share code, notes, and snippets.

@brandondove
Created January 23, 2018 21:24
Show Gist options
  • Save brandondove/b9d5102fc424f7ceaafce5219b4c4d41 to your computer and use it in GitHub Desktop.
Save brandondove/b9d5102fc424f7ceaafce5219b4c4d41 to your computer and use it in GitHub Desktop.
<?php
// Hook into the init action
add_action( 'wp', 'pj_check_page' );
// Checks to see if the page we want is being requested and adds a filter to any queries that run on the resulting page
function pj_check_page () {
if ( is_page( 'The Page Name' ) ) {
// for pagination
add_filter( 'get_previous_post_sort', 'pj_sort_it' );
add_filter( 'get_next_post_sort', 'pj_sort_it' );
// for the content displayed on this page
add_filter( 'posts_orderby', 'pj_sort_it' );
add_filter( 'posts_join', 'pj_join_it' );
add_filter( 'posts_where', 'pj_where_it' );
}
}
// Changes the order of the items returned
function pj_sort_it () {
global $wpdb;
return " $wpdb->postmeta.meta_value DESC ";
}
// Adds SQL to join the post meta table
function pj_join_it( $join = '' ) {
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
return $join;
}
// Limits the SQL reults to content with a rating
function pj_where_it( $where = '' ) {
global $wpdb;
$where .= " AND $wpdb->postmeta.meta_key = 'pj-rating' ";
return $where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment