Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
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 '11 at 11:32
    
@tollmanz - thank you, I like this a lot. Good idea on creating a default type. –  caseym Jul 12 '11 at 12:30

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'];
}
share|improve this answer
    
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 '11 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 '11 at 8:07
    
@tollmanz - Yes, I did forget to change the keys in the IF statement. –  Pippin Jul 11 '11 at 15:20
    
@kaiser - thanks for the clarification. I'm not sure why I've missed this function before. –  tollmanz Jul 12 '11 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 '11 at 11:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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