Skip to content

Instantly share code, notes, and snippets.

@EdwardBock
Last active October 15, 2021 01:06
Show Gist options
  • Save EdwardBock/2ef91f8af3f5949e1240b6acb273ce9f to your computer and use it in GitHub Desktop.
Save EdwardBock/2ef91f8af3f5949e1240b6acb273ce9f to your computer and use it in GitHub Desktop.
ReadingTime WP_Query Extension
<?php
namespace PublicFunctionOrg/WordPress/ReadingTime;
add_filter('posts_where', function($where, $wp_query){
$readingTime = $wp_query->get("reading_time", false);
if(is_array($readingTime) && isset($readingTime["compare"]) && isset($readingTime["value"]) ){
$db = new Database();
$value = intval($readingTime["value"]);
$compare = $readingTime["compare"]; // should be sanitized
$postIdsQuery = "SELECT post_id FROM {$db->table} WHERE reading_time {$compare} {$value}";
$postsTable = $db->wpdb->posts;
$where.= " AND {$postsTable}.ID IN ( $postIdsQuery ) ";
}
return $where;
}, 10, 2);
$query = new \WP_Query([
"reading_time" => [
"compare" => ">=",
"value" => 5,
],
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment