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.

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.

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!

share|improve this question
    
Is it an empty array? Update: Instead of echo, try <?php print_r( $item ); ?> to see. –  weberwithoneb May 24 '12 at 21:53
add comment

2 Answers 2

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"
)
share|improve this answer
    
Ok, if I change it to this: <?php $item = get_post_meta($post->ID, 'multiedit_Info', false); ?> <div id="info-col"> <?php print_r( $item ); ?> </div> It outputs this: Array ( [0] => ) –  ultraloveninja May 24 '12 at 22:13
    
OK, that means you have an empty array. There is nothing stored in that meta key for that post ID. –  EAMann May 24 '12 at 22:49
    
So, will this work for ALL the custom fields within the pages or just for that one? I am trying to get ALL or ANY of the custom fields with that meta info. –  ultraloveninja May 24 '12 at 23:31
    
get_post_meta() only gets meta information for a single post/page. I'm not quite sure what you're trying to accomplish now, and it's not very clear in your question. Can you please edit your question to add some clarification? Don't just list what you've tried, explain in detail what you're trying to accomplish. –  EAMann May 24 '12 at 23:39
    
Ok, I edited my question. Hopefully that will help a little. –  ultraloveninja May 25 '12 at 1:05
add comment

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!

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.