user image

Joseph Morgan
Published in : 2022-01-30

[solved] undefined $post->ID in WP_Query()

Wordpress

Hello everyone, I am facing weird error in my website I can see this error 

Warning: Undefined variable $post in /customers/2/5/e/mastratec.com/httpd.www/wp-content/themes/salient-child/skills-section.php on line 21

Warning: Attempt to read property "ID" on null in /customers/2/5/e/mastratec.com/httpd.www/wp-content/themes/salient-child/skills-section.php on line 21

the file looks like 

<?php 
	$args = [
		'post_type' => 'skills',
		'status' => 'published',
		'orderby' => 'date', 
		'order' => 'ASC',
		'posts_per_page' => -1,
	];
	$count = 0;
	$posts = new WP_Query( $args );
	if( $posts->have_posts() ) :
		?>
      <div >
		<?php 
		$count = 1 ;
		while($posts->have_posts()):
			$posts->the_post();
			?>
				<div >
          <div >
            <?php if(get_the_post_thumbnail_url($post->ID)){ ?>
              <img src="<?php echo get_the_post_thumbnail_url($post->ID); ?>" alt="Image">
            <?php } ?>
            <div >
                <h4><?php the_title(); ?></h4>
            </div>
          </div>
				</div>
			<?php
			if($count % 6 == 0){
				echo "</div> <div class='skills_container'>";
			}
			$count++ ;
		endwhile;
	else:
		echo "<h3 style='text-align: center;'> No posts to show </h3>";
	endif;
?>

This is the first time to see this error however that code is working fine on another server,
Please help

web-brackets.com

Comments

Mohamed Atef Date : 2022-01-30

Best answers

46

Best answers

46

Hi Joseph,
This might be a error because of a different PHP version or any Wordpress update, but anyway I got the issue & there are a simple way to figure this out,
You can figure this error by replacing $post->ID with a function to get the ID of the post inside the loop which is get_the_ID()
Your loop should looks like 

<?php 
		while($posts->have_posts()):
			$posts->the_post();
			?>
				<div >
          <div >
            <?php if(get_the_post_thumbnail_url(get_the_ID())){ ?>
              <img src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Image">
            <?php } ?>
            <div >
                <h4><?php the_title(); ?></h4>
            </div>
          </div>
				</div>
			<?php
			if($count % 6 == 0){
				echo "</div> <div class='skills_container'>";
			}
			$count++ ;
		endwhile;
?>

Good luck!!

Joseph Morgan Date : 2022-01-30

Thank you so much! you are amazing

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Cpanel to Webmin migration 503 error
Publish date: 2021-08-12 | Comments: 4

Tag: Wordpress

A custom WordPress Login page and URL
Publish date: 2022-02-10 | Comments: 2

Tag: Wordpress

How to display tags on each posts for customized wordpress theme?
Publish date: 2022-02-26 | Comments: 1

Tag: Wordpress

A Site Kit WordPress plugin bug.
Publish date: 2022-02-10 | Comments: 2

Tag: Wordpress

Will Google Dorking save my secure WordPress website running on HTTPS?
Publish date: 2022-02-26 | Comments: 1

Tag: Wordpress

[solved] How to change privacy policy text above place order button Woocommerce?
Publish date: 2022-01-08 | Comments: 2

Tag: Wordpress

creating custom gallery
Publish date: 2021-09-12 | Comments: 4

Tag: Wordpress

What is the best reference to the most used WordPress functions?
Publish date: 2022-02-09 | Comments: 2

Tag: Wordpress