Last active
October 15, 2021 01:06
-
-
Save EdwardBock/2ef91f8af3f5949e1240b6acb273ce9f to your computer and use it in GitHub Desktop.
ReadingTime WP_Query Extension
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
<?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