Home Forums Backend Issues (wp-admin) Post Object at random Reply To: Post Object at random

  • @hube2 I got somewhat further:

    $motd = get_field('motd');
    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    $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
            // 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 href="<?php the_permalink();?>"><?php the_title();?></a>
    } ?>

    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.