PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

how to get the value of a custom field?

  1. First, let me say that I am a PHP newbie, I am more of a RoR programmer so I am a bit lost in PHP syntax which may explain the simplicity of my question...

    Goal: I want to have a form where people can enter the price of the item they want to sell.

    How: I used a custom field, gave it a 'new' name of 'price'. I used the form and when I look at the post content, the value is displayed in the custom field section of the post edit page. Then, in my single_post page, I use the following code to display the content of the custom field and it does not work:

    <?php get_post_meta($post->ID, 'price', true); ?>

    nothing is output and if I do this:

    <?php get_post_meta(the_ID(), 'price', true); ?>

    it only display the id of the post.

    What am I missing? I am sure I am missing something simple since all the method calls seem good.

    Thx.

    Posted 14 years ago on Thursday January 14, 2010 | Permalink
  2. Hey Alain, hit me up via email with a WordPress login for this site and i'd be happy to take a look at how you have things setup and help you out with getting the syntax correct. If you don't have my email address anymore you can use the Contact Us form on this site. Thanks!

    Posted 14 years ago on Thursday January 14, 2010 | Permalink
  3. Thx Carl. Created an admin account for you.

    Posted 14 years ago on Thursday January 14, 2010 | Permalink
  4. the_ID() echoes the post ID, which is why that part does not work.

    maybe $post->ID is a global not available in your single.php template (normally it works just fine in single.php) Have you tried doing a <?php global $post; ?> before you try get_post_meta?

    I've run into this with the Carrington Theme from Alex King:
    http://carringtontheme.com/2009/06/remember-variable-scope/

    Also, you are echoing or assigning that meta value to something, right? The code you share here just gets the value, but you're not doing anything with it.

    Posted 14 years ago on Friday January 15, 2010 | Permalink
  5. That Illinois, I was just too ignorant of PHP... I just replaced the line with:
    <?php echo get_post_meta($post->ID, 'price', true); ?>

    and now everything is fine. This is very frustrating for me to move from a language were I know pretty much everything (Ruby) to something new where even the easiest things are evading me!

    Guess I will have to read a PHP book afterall!

    Posted 14 years ago on Friday January 15, 2010 | Permalink
  6. Don't read, just keep hacking, you're doing fine.

    Posted 14 years ago on Friday January 15, 2010 | Permalink
  7. Good Alain, was getting ready to look into your issue and see that illinoisharley has already helped you out :)

    Posted 14 years ago on Friday January 15, 2010 | Permalink

This topic has been resolved and has been closed to new replies.