get_post_meta Does Not Work Within The Loop

UPDATE: Please disregard this blog post. Ryan Duff gave me a huge tip that my code was not written correctly. In the code below, note that I have the line written as follows:
get_post_meta(post->ID, ‘custom_field_value’, true);
When you add the fancy dollar sign in front of “post->ID” like so “$post->ID”, the problem is fixed. Chalk it up to a late night bonehead move on my part, I guess. 🙂

I ran into a problem this morning.

I was trying to use get_post_meta() within the loop and was unable to return the custom field value.  The solution was to use “get_the_id()” rather than “post->ID” as shown below.

 

$my_query = new WP_Query('showposts=1');
	while ($my_query->have_posts()) : $my_query->the_post();
		get_post_meta(get_the_id(), 'custom_field_value', true);  // This works!
		get_post_meta(post->ID, 'custom_field_value', true);  // This DOES NOT work!
	endwhile;

Thanks to this post for providing the solution.

Posted in ,

The Mighty Mo! Design Co.

RSS From Toby’s Blog

  • How Digital Ocean Cost Us Thousands of Dollars in a Single Day
    Here’s the rub for me: My belief is that bad stuff happens, and you need to own it. Digital Ocean definitely did not own it in this case. I wonder how the original founders of Digital Ocean would have reacted had they been in charge… For example, wouldn’t it be nice if an actual human… […]
  • Where uncertainty exists, don’t order specific.
    4 Idiot Case Studies with 2 simple age-old business lessons. The post Where uncertainty exists, don’t order specific. appeared first on Toby Cryns, WordPress Trainer in Minneapolis, MN.
  • “Now THAT was a good meeting!”
    A single effective meeting can change the course of history for a company. Imagine if you could lead effective meetings EVERY DAY! You can, but it takes curiosity & practice. This article is a good place to start your journey to a more effective you. The post “Now THAT was a good meeting!” appeared first […]

More posts from themightymo.com

How to install a Cloudflare SSL on Siteground

How to install a Cloudflare SSL certificate on Siteground

By The Mighty Mo! Design Co. | April 12, 2023

Hey there! In this video, I’ll show you how to install a Cloudflare SSL certificate on your SiteGround website. I’ve noticed that Let’s Encrypt sometimes doesn’t renew properly due to conflicts with Cloudflare, so this is a great solution. We’ll use TCwebsecurity.com as the guinea pig. I’ll walk you through the process step-by-step. Once we…

wayback machine

How to Download a Website from Wayback Machine

By The Mighty Mo! Design Co. | March 27, 2023

Today I ran into an issue where a bunch of website files were deleted off the server, and I was called upon to restore the old files. After first pursuing other options (e.g. backups), I landed on Wayback Machine as the next best choice. Thankfully, there’s a handy command line tool for downloading any site…

wordpress website maintenance

My WordPress Maintenance Process

By The Mighty Mo! Design Co. | March 2, 2023

A few quick things: My WordPress Maintenance Process Demo, Part 1 Transcript: You’ll see immediately after logging in, you see the 11 updates needed as well as some messages. I’m just gonna quickly read. It looks like this. I don’t need to worry about. Are you enjoying Monster Insights? Not really <laugh>. What’s to enjoy…

RSS From Toby’s Blog

  • How Digital Ocean Cost Us Thousands of Dollars in a Single Day
    Here’s the rub for me: My belief is that bad stuff happens, and you need to own it. Digital Ocean definitely did not own it in this case. I wonder how the original founders of Digital Ocean would have reacted had they been in charge… For example, wouldn’t it be nice if an actual human… […]
  • Where uncertainty exists, don’t order specific.
    4 Idiot Case Studies with 2 simple age-old business lessons. The post Where uncertainty exists, don’t order specific. appeared first on Toby Cryns, WordPress Trainer in Minneapolis, MN.
  • “Now THAT was a good meeting!”
    A single effective meeting can change the course of history for a company. Imagine if you could lead effective meetings EVERY DAY! You can, but it takes curiosity & practice. This article is a good place to start your journey to a more effective you. The post “Now THAT was a good meeting!” appeared first […]