0

Posts have a custom field with key time and a value. In a template I want to query the posts that have the time key and a value smaller than 30. Whatever I have done so far didn't work.

Below is the current situation that I think it should work, but it doesn't. It outputs all posts with a key times and doesn't do anything with the value. It is kind of a copy from the Codex where is written:

Display posts where the custom field value is a number. Displays only posts where that number is less than 10. (WP_Query uses this equation to compare: $post_meta . $args['meta_compare'] . $args['meta_value'], where '$post_meta' is the value of the custom post meta stored in each post; the actual equation with the values filled in: $post_meta < 10)

$args = array(
'post_type' => 'post',
'meta_key' => 'number',
'meta_value_num' => 10,
'meta_compare' => '<',
);
$query = new WP_Query( $args );

This is my code. How to fix it?

<?php /* Template Name: posts less than 30 secs */
get_header(); 
?>
<div id="content-wrap">

  <?php
  $args = array(
   'post_type' => 'post',
   'meta_key' => 'time',
   'meta_value_num' => 30,
   'meta_compare' => '<=',
   );

$times = new WP_Query($args);  
 if ($times->have_posts()) :
echo '<h1 class="archive_title">Posts with less than 20 value in time key</h1>'; ?>
   <div class="post">
    <?php while ($times->have_posts()) : $times->the_post(); ?>
     <div id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?>>
       <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail( array(100,100) ); ?></a>
        <h2 class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
          <div class="entry"><?php the_excerpt();  ?></div>
     </div>
    <?php endwhile; ?>
  </div>
<?php get_template_part( 'pagination' ); ?>
<?php wp_reset_query(); ?>
 <div class="cleaner">&nbsp;</div>
 <?php endif; ?>
</div>
<?php get_footer(); ?>
  • If it doesn't do what you expected, what does it do? Do you have an example of a value it should be testing against? wp_reset_query should only be used with the query_posts function, you should use wp_reset_postdata() instead. Have you confirmed there are indeed posts that have a post meta with the exact key time? – Tom J Nowell Sep 14 '18 at 23:56
  • @TomJNowell It does do: It outputs all posts with a key times and doesn't do anything with the value. Do you have an example of a value it should be testing against: 'meta_value_num' => 30, 'meta_compare' => '<=',. wp_reset_postdata() OK thanks for the tip. Yes there are posts with the exact time key but I am looking for the ones that are smaller than 30. – WendiT Sep 15 '18 at 9:29
0

The args are wrong. The correct code is:

$args = array(
'post_type' => 'post',
'meta_key' => 'time',
'meta_value' => '30',
'meta_compare' => '<',
);

The data is stored as strings and not as numbers so no need to use meta_value_num.

  • 1
    Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Sep 15 '18 at 11:22

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.