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 want to list all the values of a custom metabox in a custom post type.

Here is the code to get the one meta box in a single post

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

but say I want to list all the meta boxes in the home page or a dropdown menu in a search form not just in a single page

the metabox code

 add_meta_box(
    'Location_metabox',
    __( 'Location ', 'twentyeleven' ),
    'institution_location_metabox_output',
    'institution',
    'side'
);

Thanks

Solution

I tried to use WP_Query and it works fine.

<?php
    $args = array('post_type' => 'institution');
    $the_query = new WP_Query($args);

    while ( $the_query->have_posts() ) : $the_query->next_post();
    $id= $the_query->post->ID;
    $location = get_post_meta($id, 'institution_location', true);
    echo $location;
    endwhile;
?>
share|improve this question
add comment

2 Answers

Try using get_post_custom, that will return an array of all meta values linked to the post

share|improve this answer
    
Thanks hannit I tried WP_Query and it works for me, updated solutions. I think get_post_custom will work for a single post. –  Ahmad Ajmi Dec 3 '12 at 9:24
    
you are right of course –  hannit cohen Dec 3 '12 at 9:27
add comment
up vote 0 down vote accepted

I tried to use WP_Query and it works fine.

<?php
    $args = array('post_type' => 'institution');
    $the_query = new WP_Query($args);

    while ( $the_query->have_posts() ) : $the_query->next_post();
    $id= $the_query->post->ID;
    $location = get_post_meta($id, 'institution_location', true);
    echo $location;
    endwhile;
?>
share|improve this answer
add comment

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.