WordPress error on latest_post() function

Issue

im my single.php file I have two functions that displays related posts to the one currently reading. When I add a new post and do not add tags to it, an error will appear when I try to preview it.

Error thrown
Call to a member function have_posts () on null

When I add tags to it, everything displays fine.

<?php
                //for use in the loop, list 5 post titles related to first tag on current post
                $tags = wp_get_post_tags($post->ID);
                if ($tags) :
                
                $tag_ids = array();
                foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
                $args=array(
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>6,
                'caller_get_posts'=>1
                );
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) :
                ?>
                
                <div class="d-block d-lg-none text-center" >
<?php get_sidebar(); ?>
            <div class="col-12 col-lg-7 offset-lg-1">
                <div id="powiazane">
                    <h2> Powiązane artykuły: </h2>
                    <div class="card-deck mb-50px">
                    <?php
                    $post_wrap = 0; 
                    while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class('card border-0 ' . $termsString); ?>>
                        <img class="card-img-top secondary-post-img p-3" src="<?php echo the_post_thumbnail_url('post-thumb'); ?>" alt="<?php the_title_attribute(); ?>">
                        <div class="card-body">
                            <a href="<?php the_permalink(); ?>" class="stretched-link" alt="<?php the_title_attribute(); ?>">
                                <?php
                                    if(get_field('tytul_krotki'))
                                    {
                                    echo '<h3>' . get_field('tytul_krotki') . '</h3>';
                                    } else {
                                    echo '<h3>' . get_the_title() . '</h3>';
                                    }
                                ?>
                            </a>
                            <div class="small text-muted">
                                <p class="mb-0">
                                    <?php the_field('miasto'); ?> | <?php echo hubdab_relative_time(); ?>
                                </p>
                            </div>
                            <p class="mt-25px"><?php the_field('lid_krotki') ?> </p>
                        </div>
                        <div class="card-footer py-0">
                        </div>
                    </article><!-- #post-## -->
                    <?php
                    
                    if ($post_wrap % 2 == 1){
                        echo '<div class="w-100 d-none d-sm-block"><!-- wrap every 2 on sm+--></div>';

                    }   
                    $post_wrap++; ?> 
                    <?php
                    endwhile;
                    ?>

Solution

You have a problem in this code

$tags = wp_get_post_tags($post->ID);
                if ($tags) :
                 $tag_ids = array();

                 foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
                $args=array(
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>6,
                'caller_get_posts'=>1
                );
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) 

$tags = wp_get_post_tags($post->ID) will return

(array|WP_Error) Array of WP_Term objects on success or empty array if no tags were found. WP_Error object if ‘post_tag’ taxonomy doesn’t exist.

So if you check for if($tags) always will be true, as WP_Error will be true in case of no tags.

You can modify your condition to execute the query as:

if (!is_wp_error($tags) and count($tags)):

 ....

Hope this help

Answered By – Manuel Glez

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published