Created
January 19, 2021 17:40
-
-
Save marcofbb/4616209eb0b2132281116713d4b5c28e to your computer and use it in GitHub Desktop.
WordPress extend results append POST in Tag list
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 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