Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
deleted 58 characters in body
Source Link
Pieter Goosen
  • 55.5k
  • 23
  • 118
  • 215

Evening All.

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$stored_value to the $now or not?

Any help would be great!

Cheers

Evening All.

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!

Cheers

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!

Source Link

Query Posts based on custom field value

Evening All.

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!

Cheers