2

I have 15 custom fields that I use to generate reviews for my site. Currently I access each field like this:

//Buy or rent, type home, price
                 if ( get_post_meta(get_the_ID(), 'survey_home_type', true) ) {
                    echo get_post_meta(get_the_ID(), 'survey_home_type', true) . " - ";
                 } 
                 if ( get_post_meta(get_the_ID(), 'survey_buy_or_rent', true) ) {
                    echo get_post_meta(get_the_ID(), 'survey_buy_or_rent', true) . " for ";
                 }
                 if ( get_post_meta(get_the_ID(), 'survey_purchase_price', true) ) {
                    echo get_post_meta(get_the_ID(), 'survey_purchase_price', true);
                 } elseif ( get_post_meta(get_the_ID(), 'survey_rent', true) ) {
                    echo get_post_meta(get_the_ID(), 'survey_rent', true);
                 } 

Is there a better way to do this? I tried using get_post_custom but it seems to mainly deal with arrays, not single items. I could also declare all the variables ahead of time, such as $rent, $purchase_price. I would like to hear any advice!

2 Answers 2

3

I would write a function to handle the monotony of this task.

function my_print_meta($id, $key, $alternate = '')
{
    if($value = get_post_meta($id, $key, true))
        echo $value;
    else
        echo $alternate;
}

Notice in the function that I only call get_post_meta once and store the value in a variable so that two separate database queries don't need to be made. With this function set, I would then call it in a template file using:

my_print_meta(get_the_ID(), 'survey_home_type', 'No Home Type');

Now, realize that this function, with the way that it manages the "alternate" text might not work for your template, but this is just an idea to get you going

2
  • 1
    I agree with wrapper function approach but I'd change condition to false !== $value. A lot of things loosely evaluate to false - empty strings, empty arrays. Storing such in option is technically different from option having nothing stored in it.
    – Rarst
    Jul 12, 2011 at 11:32
  • @tollmanz - thank you, I like this a lot. Good idea on creating a default type.
    – caseym
    Jul 12, 2011 at 12:30
2

You could use get_post_custom()

It's not a lot different, but a little less code.

$meta = get_post_custom( $post->ID );
if( $meta ) 
{ 
    echo $meta['survey_home_type'];
    echo $meta['survey_buy_or_rent'];
    echo $meta['survey_rent'];
}
7
  • I've not used get_post_custom before as I prefer to query the meta data separately. Did you forget to change the keys in the conditional statements or is there a reason that you are always checking for 'survey_home_type'? I do like that you are only making one query here.
    – tollmanz
    Jul 10, 2011 at 23:59
  • @tollmanz get_post_custom() is the "native" function behind get_post_meta() and returns an array of all attached meta data. The only thing that could be changed is the if statement. Not necessary to call that for every line, but i guess the above is for demonstrational purpose only.
    – kaiser
    Jul 11, 2011 at 8:07
  • @tollmanz - Yes, I did forget to change the keys in the IF statement.
    – Pippin
    Jul 11, 2011 at 15:20
  • @kaiser - thanks for the clarification. I'm not sure why I've missed this function before.
    – tollmanz
    Jul 12, 2011 at 1:25
  • Actually, the generic function behind get_post_meta() is now get_metadata(). Whichever you use, you never do more than one query per post, as the metadata is cached.
    – scribu
    Jul 12, 2011 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.