0

Im using ACF and trying to show a post type title based on the current time. So I have my post type entry with start_time and end_time and my conditional is showing my div.onAir, but Im getting this Recoverable Fatal Error. Here is my code:

<?php
    $time_now = date("g:i a");
    $shows = get_posts( array(
      'post_type' => 'shows',
      'meta_query' => array(
        array(
          'key' => 'start_time',
          'compare' => '<=',
          'value' => $time_now,
          'type' => 'DATETIME',
        ),
        array(
          'key' => 'end_time',
          'compare' => '<=',
          'value' => $time_now,
          'type' => 'DATETIME',
        )
      ),
    ));
  if( $shows ) {
    foreach( $shows as $show ){ ?>
       <div class="onAir">
         Currently On Air: <?php echo the_title($show); ?>
       </div>
  <?php
  wp_reset_query();
}
} ?>

I feel Im close, but I haven't seen this particular error before. Any help would be appreciated! Thanks.

UPDATE:

Tom changed my code up to use WP_Query. I had an issue with the timestamps in ACF and WP matching up. Once I figured that out, this code renders the correct comparison of show posts that I was going for. Please note that I used date_i18n('g:i a'); which fixed my localization timestamp. Thanks Tom and Yuri!

<?php

$time = date_i18n('g:i a');
$shows = get_field('station_shows', false, false);
$query = new WP_Query(array(
  'post_type'       => 'shows',
  'posts_per_page'  => 1,
  'post__in'            => $shows,
  'meta_query' => array(
    'relation' => 'AND',
      array(
          'key'     => 'start_time',
          'compare' => '<=',
          'value'   =>  date_i18n('g:i a', $time),
          'type'    => 'TIME',
      ),
      array(
          'key'     => 'end_time',
          'compare' => '>=',
          'value'   => date_i18n('g:i a', $time),
          'type'    => 'TIME',
      )
  ),
 ) );
 if ( $query->have_posts() ) { while( $query->have_posts() ) { 
 $query-   >the_post();

 echo '<div class="onAir"><h3>Currently On Air @ ';
 the_title();
 if (get_field('dj', $query->ID)) {
           $dj = get_field('dj');
           echo ' w/ ';
        echo $dj;
    }
     echo '</h3></div>';
    } wp_reset_postdata();
  }
 ?>
3
  • Note that you should set suppress_filters to false when using get_posts to avoid the performance hit, otherwise using WP_Query is always better. Also, wp_reset_query shouldn't be used here, it's for cleanup after query_posts
    – Tom J Nowell
    Apr 3, 2020 at 21:25
  • Thanks Tom, yes I changed it to new WP_Query and the error changed to Object of class WP_Tax_Query Apr 3, 2020 at 21:27
  • 1
    It sounds like you kept the foreach the same
    – Tom J Nowell
    Apr 3, 2020 at 21:31

2 Answers 2

2

the_title doesn't work that way:

the_title( $before, $after, $echo );

$before is the text that comes before the title, but you didn't give it a string/text, you gave it a post object. Post objects aren't strings, PHP doesn't know what to do so it stops and prints an error instead.

For the_title to work, you need to setup the current postdata. Normally a standard loop does this by calling the_post on the query, but you've chosen to use get_posts instead.

This is what a standard post WP_Query post loop should look like:

$query = new WP_Query([ ... ]);
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        //.... display post here
    }
    wp_reset_postdata();
}
0

You are calling WordPress functions incorrectly. I fixed, try this code.

<?php
    $time_now = date( "g:i a" );
    $shows    = get_posts( array(
        'post_type'  => 'shows',
        'meta_query' => array(
            array(
                'key'     => 'start_time',
                'compare' => '<=',
                'value'   => $time_now,
                'type'    => 'DATETIME',
            ),
            array(
                'key'     => 'end_time',
                'compare' => '<=',
                'value'   => $time_now,
                'type'    => 'DATETIME',
            )
        ),
    ) );

    if ( $shows ) :
        foreach ( $shows as $show ) :
    ?>

    <div class="onAir">
        Currently On Air: <?php echo get_the_title($show->ID); ?>
    </div>

    <?php
        endforeach;
        wp_reset_postdata();
    endif;
?>
5
  • Okay that took away the error, but now is showing the title of the parent page which template has the time query in....Im trying to get the "shows" title to show where the_title() is Apr 3, 2020 at 21:46
  • Yes, my mistake, updated the code.
    – Neolot
    Apr 3, 2020 at 23:05
  • Can you explain in your question what you did? If this is just a copy paste solution then it doesn't really answer the question or explain/teach anything, the question hasn't really been answered
    – Tom J Nowell
    Apr 4, 2020 at 10:41
  • I changed the title output code to make it work correctly.
    – Neolot
    Apr 4, 2020 at 10:55
  • Well the question of the error has be fixed, but the output still isn't accurate. I am now populating an on air show (post type), but its the wrong show for the time frame....so Im still off somewhere. Thanks for you help Tom and Yuri Apr 5, 2020 at 17:26

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.