I have a custom field (not using ACF) attached to all my posts (standard WP posts) called 'Read Time'. Basically a function calculates the read time of each post and enters a number into that field. Now I need to create a search form where I can filter those using one of 3 options and sort them by date, title etc. Filters should be something like: 'between 1-5 minutes', 'between 5-10 minutes' and '11 and above'. As my PHP skills are not great to build complex queries and arrays, I searched around and came across with 2 articles. One of them is this and the other is this .

I have two questions: a) How can I query all posts to check read times and filter one a click/selection of an option? I wrote a basic select like the one below but can't figure out how to do anything further"

 <select name="readtimefilter"> 
     <option value="5">0-5 Minutes</option>
     <option value="10">6-10 Minutes</option>
     <option value="11">11+ Minutes</option>

b) Can someone explain how the code given in the second link works as I cannot see it using any queries such as WP_Query?

Many thanks in advance!

Edit (18/10/2017): Managed to get the filters working thanks to this answer. Here's how I get that to work.

if( isset( $_POST['readtimefilter'] ) && $_POST['readtimefilter'] ) {
  $read_time = (int)$_POST['readtimefilter'];
  if ($read_time == 5) {
    $args['meta_query'][] = array(
            'key' => 'post_read_time',
      'value' => $read_time+1,
            'type' => 'numeric',
            'compare' => '<'
  elseif ($read_time == 10) {
    $args['meta_query'][] = array(
      'key' => 'post_read_time',
      'value' => array( 6, 11 ),
        'type' => 'numeric',
        'compare' => 'between'
  else {
    $args['meta_query'][] = array(
      'key' => 'post_read_time',
      'value' => $read_time,
      'type' => 'numeric',
      'compare' => '>'
  • please do not point to external resources in your question and ask about them. no one is likely to bother reading them – Mark Kaplun Oct 16 '17 at 5:23
  • Only reason I didn't post the content of external links is to avoid the question getting too long. I am happy to cut and paste everything here if that's preferred. And you could just comment it rather than down voting. – Pradeep Oct 16 '17 at 5:31
  • I could also just downvote and not explain... the question is going to be -1 from me as long as it is a bad one – Mark Kaplun Oct 16 '17 at 7:33
  • I suggest you also find a stack related on interpersonal skills and try to ask questions there. seems like you have many questions about how to communicate with people over the internet that need answers – Mark Kaplun Oct 16 '17 at 10:36
  • The answer below clearly shows that some people DO 'bother' reading external resources. As Sagan said "There is no such thing as a dumb question". I hope you know the rest of the saying. If you couldn't be 'bothered' to read the linked resources, all you had to do was move on and leave it someone else to bother, as no one forced you to do anything. That's the proper etiquette, especially in forums such as these. – Pradeep Oct 18 '17 at 11:44

A full, working-code style answer is a bit out of scope here, but here are some suggestions for your two questions:

  1. Custom fields are saved as postmeta. So you'll want to set up a simple form with one text input and GET method. In your PHP to process the form, check whether your $_GET variable is set. If not, display the empty form. If so, use a WP_Query to find all posts with the given parameters. Here's an excellent answer that shows how to do a greater-than-or-equal-to comparison on postmeta.

  2. The code from your second link only works within the Loop. The main query has already run by the time this code executes, so it appears to be meant to run only on an Archive which would already have a number of posts fetched from the database.

  • Thank you so much for the input. Funnily I came across with the same answer and managed to get the filters working. Not sure how elegant it is as solution but it's doing the job. I'll edit my question to include that code segment. But I was still trying to figure out how the second code works and now I know the answer thanks to you. – Pradeep Oct 18 '17 at 11:18
  • Sorry I've lied :-) The answer that I came across was this (wordpress.stackexchange.com/questions/83928/…). But still your input works as the answer. – Pradeep Oct 18 '17 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.