0

Currently building a site and need to create a 'Schedule' to display a Presenter that is on air at a particular time.

Using ACF plugin to enter a time that the presenter goes on air which saves the custom field value as a timestamp.

For some reason, I cant get the query to display the presenter that is on air at a certain time. It seems to just show the presenter with the highest timestamp value when compared to the current time timestamp. A simplified version of the code is below.

<?php $args = array(
    'post_type' => 'presenter',
    'posts_per_page' => '1',
    'meta_key' => 'on_air',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'

);


$airquery = new WP_Query( $args ); ?>

<?php if ( $airquery->have_posts() ) : while ( $airquery->have_posts() ) : $airquery->the_post(); ?>

    <?php
        $now = strtotime(date('d-m-Y H:i:s')); // get current timestamp
        $stored_value = get_field('on_air');   // The saved post timestamp comparing to current timestamp
    ?>

    <?php if( $now > $stored_value )
        {
            echo the_title();
        }
    ?>

<?php endwhile; endif; wp_reset_postdata(); ?>

As you can see, i'm querying the posts in the post type, and want to order by meta value, which also doesnt seem to be working.

All I get returned is the post with the greatest timestamp value above the current timestamp.

All very strange. Dont know wether I need to somehow get the value of all the posts and then compare the $stored_value to the $now or not?

Any help would be great!

0

Why not pull the post you need, rather than pull a post and then check the value of the meta key?

$now = strtotime(date('d-m-Y H:i:s')); // get current timestamp

$args = array(
    'post_type' => 'presenter',
    'posts_per_page' => '1',
    'order' => 'DESC',
    'meta_query' => array(
        array(
            'key'     => 'on_air',
            'value'   => $now,
            'compare' => '>=',
        ),
    ),
);

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.