get_post_meta just returns Array

Here’s what I am trying to do:

I am trying to get one page in wordpress to pull data from custom fields in other pages. So that when a user creates a page and uses a particular custom field (in this case multiedit_Info), it will populate in the “target” page.

Read More

Here’s my code that I am working with:

<?php $item = get_post_meta($post->ID, 'multiedit_Info', false); ?>
<div id="info-col">
<?php echo $item; ?>
</div>

I am currently using a plugin that create content blocks by using custom fields. And I want to get it so that a user can just create a new page with the custom fields and then have it display in an array on another page.

Not too sure if I am setting this up correctly or not. This is the first time I’ve ever used it so any help is appreciated. Thanks!

Related posts

Leave a Reply

2 comments

  1. When you set the third parameter of get_post_meta() to false, it returns an array of all the values mapped to that key – setting this parameter to true returns a single value.

    So your code is correct. You’d get the exact same thing if you did something like this:

    $test = array( "1", "2", "3" );
    echo $test; // prints "Array"
    

    If you want to view the contents of your array, you’ll need to use print_r():

    $item = get_post_meta( $post->ID, 'multiedit_Info', false );
    echo print_r( $item );
    

    If you were to do this with the $test array from my above example, you’d see:

    Array
    (
        [0] => "1"
        [1] => "2"
        [2] => "3"
    )
    
  2. Ok, I found the solution I was looking for here:

    getting all values for a custom field key (cross-post).

    By using the function listed in the above post and then calling back the info in my template by using this:

    <?php $my_info = get_meta_values( 'multiedit_Info', 'page' ); ?>

    and then outputting those in a foreach:

    <?php foreach ( $my_var as $item) :
    echo "<p>$item</p>";
    endforeach; ?>
    

    Grabbed all the custom fields within all of the pages that are using them and then outputted them into a one page.

    Thanks for the help!