Home > Enterprise >  Wordpress error on latest_post() function
Wordpress error on latest_post() function

Time:06-14

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  >
<?php get_sidebar(); ?>
            <div >
                <div id="powiazane">
                    <h2> Powiązane artykuły: </h2>
                    <div >
                    <?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  src="<?php echo the_post_thumbnail_url('post-thumb'); ?>" alt="<?php the_title_attribute(); ?>">
                        <div >
                            <a href="<?php the_permalink(); ?>"  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 >
                                <p >
                                    <?php the_field('miasto'); ?> | <?php echo hubdab_relative_time(); ?>
                                </p>
                            </div>
                            <p ><?php the_field('lid_krotki') ?> </p>
                        </div>
                        <div >
                        </div>
                    </article><!-- #post-## -->
                    <?php
                    
                    if ($post_wrap % 2 == 1){
                        echo '<div ><!-- wrap every 2 on sm --></div>';

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

CodePudding user response:

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

  • Related