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"> </div>
<?php endif; ?>
</div>
<?php get_footer(); ?>
wp_reset_query
should only be used with thequery_posts
function, you should usewp_reset_postdata()
instead. Have you confirmed there are indeed posts that have a post meta with the exact keytime
?