Home › Forums › Backend Issues (wp-admin) › Post Object at random › Reply To: Post Object at random
@hube2 I got somewhat further:
<?php
$motd = get_field('motd');
?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
<?php
$molecule_month = get_the_terms($motd->ID , 'molecules_of_the_month' );
$month = array_pop($molecule_month);
$molecule_link = get_term_link( $month->term_id, 'molecules_of_the_month' );
// Set the post type here, and sort them randomly
// molecule of the month presented as random molecule of the day.
$args = array(
'post_type' => 'molecule',
'posts_per_page'=> 1,
'post_category' => 'molecule_of_the_month',
'order_by' => 'rand',
);
// Initiate a custom query
$cpt_query = new WP_Query($args);
// If the query has any post, start the loop
if($cpt_query->have_posts()){
while($cpt_query->have_posts()){
// Output a link and a thumbnail of the post
$cpt_query->the_post(); ?>
<div class="random-post">
<div class="motd-img">
<a href="<?php echo $molecule_link ?>">
<?php echo get_the_post_thumbnail( $motd->ID, 'full');?>"/>
</a>
</div>
<a href="<?php the_permalink();?>"><?php the_title();?></a>
</div><?php
}
} ?>
</div>
But got this error
: Uncaught TypeError: array_pop(): Argument #1 ($array) must be of type array, bool given in /Users/jasper/code/site/wp-content/themes/theme/blocks/random-molecule/block.php:30
Why would ID be null? It refers to $month = array_pop($molecule_month);
and the field has been set before that line. And we do have molecules of the day(based on month) and molecules of the month.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.