• Resolved ultraloveninja

    (@ultraloveninja)


    So, I am trying to get a page within my wordpress template to get all custom fields with the key of multiedit_Info to populate within another page. When I do so, all that comes back is Array.

    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>

    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!

Viewing 15 replies - 1 through 15 (of 16 total)
  • You cannot echo and array directly. You probably need to iterate over the array. Please try this:

    <?php $items = get_post_meta($post->ID, 'multiedit_Info', false); ?>
    <div id="info-col">
    <?php foreach ( $items as $item) {
       echo "<p>$item</p>";
    } ?>
    </div>
    Thread Starter ultraloveninja

    (@ultraloveninja)

    When I add that code I get a PHP error

    Parse error: syntax error, unexpected T_ENDWHILE...on line 47

    Line 47 on my page template is the end while end if:

    <?php endwhile; endif; ?>

    Sorry, didn’t realize you were using that syntax. Please try this:

    <?php $items = get_post_meta($post->ID, 'multiedit_Info', false); ?>
    <div id="info-col">
    <?php foreach ( $items as $item) :
       echo "<p>$item</p>";
    endforeach; ?>
    </div>
    Thread Starter ultraloveninja

    (@ultraloveninja)

    Well, now it’s just blank.

    Does it just return ALL or ANY of the custom fields for ALL pages or just for that one page?

    I am try to get it so that one particular page arrays out ALL of particualr fields within other pages.

    get_post_meta() only selects custom fields from the post whose id is $post->ID.

    I think this article explains how to do what you want:

    https://wordpress.stackexchange.com/questions/9394/getting-all-values-for-a-custom-field-key-cross-post

    Thread Starter ultraloveninja

    (@ultraloveninja)

    Ok, that seems to be what I need, but I am confused on what would need to be replaced to fit within my code?

    Would I need to replave anything here with the $key or the %s?

    function get_meta_values( $key = '', $type = 'post', $status = 'publish' ) {
        global $wpdb;
        if( empty( $key ) )
            return;
        $r = $wpdb->get_col( $wpdb->prepare( "
            SELECT pm.meta_value FROM {$wpdb->postmeta} pm
            LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
            WHERE pm.meta_key = '%s'
            AND p.post_status = '%s'
            AND p.post_type = '%s'
        ", $key, $status, $type ) );
        return $r;
    }

    and would I need to replace YOURKEY with my meta key here as well?

    $my_var = get_meta_values( 'YOURKEY' );

    If I do an echo on the $my_var as is above, it doesn’t seem to work.

    $my_var = get_meta_values( 'YOURKEY' );

    ‘YOURKEY’ must be YOUR KEY, ie, ‘multiedit_Info’.

    The function does not need to be changed.

    Thread Starter ultraloveninja

    (@ultraloveninja)

    Ah HA!

    Ok, now we are back to where it outputs Array if I do this:

    <?php
    	$my_var = get_meta_values( 'multiedit_Info' ); ?>
    	<?php echo $my_var; ?>

    If I do this:

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

    It comes back blank. The foreach should kick out the values within the meta key? Or do I need to get it to kick out a different way?

    Sorry about all the questions. This is the first time I’ve ever hacked up WP this far…

    Are you querying Posts, or a Custom Post Type? If a Custom Post Type, you need to put the post type in the call to get_meta_values(). Assume the type is ‘reviews’, then the call would be this:

    $my_var = get_meta_values( 'multiedit_Info', 'reviews' ); ?>
    Thread Starter ultraloveninja

    (@ultraloveninja)

    The custom field is in 2 pages. The name of the field is multiedit_Info and the value of that field has HTML code that is inserted by another plugin.

    I guess thats what I am getting confused on since I am not sure what is what. And I am basically trying to get the values of the field (in this case HTML) to display on another page.

    So basically if I have 3 pages all with the same custom field of multiedit_Info, but the value of each multiedit_Info is different on each page. Then I am trying to get those values to output on a single page.

    Please distinguish between Posts and Pages.
    The type of Posts is ‘post’. You add or edit Posts at Admin->Posts.

    The type of Pages is ‘page’. You add or edit Pages at Admin->Pages.

    If you have a Custom Post type, it will have a different place to add or edit them on the Admin menu.

    Use the type where the custom field is located.

    Thread Starter ultraloveninja

    (@ultraloveninja)

    Well, son of a B.

    I changed this:

    function get_meta_values( $key = '', $type = 'post', $status = 'publish' )

    to $type=’page”

    and it seems to be working now.

    Thanks for your help! I have a better understanding of this now and I hope this comes in handy to anyone else in the future!

    Don’t change the function! Change your call to the function.

    Put the function back to thw way it was and use this in the call:

    $my_var = get_meta_values( 'multiedit_Info', 'page' ); ?>
    Thread Starter ultraloveninja

    (@ultraloveninja)

    Ah, ok.

    Just figured that’s where it needed to be changed to look for pages instead of posts. But I did that and it still seems to be working.

    Again, thanks for your help!

    If your problem has been solved, please use the dropdown on the right to mark this topic ‘Resolved’ so that anyone else with this question can see that there is a solution.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘get_post_meta just returns Array’ is closed to new replies.