admin管理员组文章数量:1277333
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();
}
?>
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();
}
?>
Share
Improve this question
edited Apr 8, 2020 at 17:30
EdgarAlexPoe
asked Apr 3, 2020 at 21:14
EdgarAlexPoeEdgarAlexPoe
1114 bronze badges
3
|
2 Answers
Reset to default 2the_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();
}
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;
?>
本文标签: custom post typesRecoverable Fatal ErrorObject of class WPPost could not be converted to string
版权声明:本文标题:custom post types - Recoverable Fatal Error - Object of class WP_Post could not be converted to string 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741288217a2370397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
suppress_filters
to false when usingget_posts
to avoid the performance hit, otherwise usingWP_Query
is always better. Also,wp_reset_query
shouldn't be used here, it's for cleanup afterquery_posts
– Tom J Nowell ♦ Commented Apr 3, 2020 at 21:25foreach
the same – Tom J Nowell ♦ Commented Apr 3, 2020 at 21:31