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 21Warning: 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><?phpif($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

51

Best answers

51

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><?phpif($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

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

Tag: Wordpress

Way to disable Block URLs for WordPress Posts
Publish date: 2022-02-26 | Comments: 2

Tag: Wordpress

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

Tag: Wordpress

Wordpress update button returns 404 [SOLVED]
Publish date: 2021-06-26 | Comments: 2

Tag: Wordpress

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

Tag: Wordpress

Add custom fields in user profile and display in Dashboard
Publish date: 2021-07-27 | Comments: 6

Tag: Wordpress

Issue with generating Thumbnails for PDF file format.
Publish date: 2022-02-26 | Comments: 1

Tag: Wordpress