Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress extend results append POST in Tag list
function extend_search_tag($query){
global $wpdb;
if(is_tag()){
// GET LIMIT RANGE OF ORIGINAL SQL QUERY
preg_match('@(LIMIT [0-9]+, [0-9]+)@', $query, $limitData);
$limit = $limitData[1];
$query = str_replace($limit, '', $query);
$query = str_replace('SQL_CALC_FOUND_ROWS', '', $query);
// SELECT GET POST UNION ON ORIGINAL QUERY
$query_most_views = "SELECT $wpdb->posts.* FROM $wpdb->posts INNER JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) WHERE 1=1 AND ( $wpdb->postmeta.meta_key = 'views' ) AND $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish') ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT 0, 160";
// CREATE NEW SQL QUERY WITH UNION
$query = "SELECT SQL_CALC_FOUND_ROWS UnionPost.* FROM (({$query}) UNION ({$query_most_views})) as UnionPost {$limit}";
}
return $query;
}
add_filter( 'posts_request', 'extend_search_tag' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment