Recoverable Fatal Error - Object of class WP_Post could not be converted to string
-
19-04-2021 - |
문제
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();
}
?>
해결책
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();
//....
}
wp_reset_postdata();
}
다른 팁
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;
?>