0

A friend of mine wants the address in his footer to be editable. So I thought to add Custom Fields to a post and then, in the footer call that post (named "Info Post") and get the address from a custom field.

I can successfully get the ID of the post but when I try to get the post meta with

echo get_post_meta($post->ID, 'street_address', true);

nothing happens.

Could someone explain where I am going wrong? Or if anyone has an idea of how better to do this I'm all ears.

Thanks for any all help!

The code I was using to get $post->ID was requested so I have added it below I have attempted to reach the 'street_address' field in a variety of ways but none of them have worked. When I echo $post->ID I do get the correct ID though.

<?php 

    $queryObject = new WP_Query( array(
                'title' => 'Business Info',
                'posts_per_page' => 1,
                ));

?>
<?php if ( $queryObject->have_posts() ) : ?>
<?php while ( $queryObject->have_posts() ) : $queryObject->the_post(); ?>

                    <?php 
                        // print_r($post);
                        $street_address = get_post_meta($post->ID, 'street_address', true);
                    ?>
            <div class="large-6 columns">
                <h3>Our office location is at <?php echo $street_address;?>.</h3>
                <!-- Image Here -->
                <p>
                    <?php echo $post->ID; ?>
                    <?php 
                        // print_r($post);
                        $street_address = the_field('street_address');
                        echo $street_address;
                        the_field('street_address');
                    ?>
                    <br>
                    We'd love to see you there!
                </p>
            </div>

<?php endwhile;?>
<?php else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>  
  • Do you have ACF pro? If not would you consider buying it? Well worth the investment, and it means you get the Options Page addon, which is perfect for things like this - global settings that aren't attached to any particular page/post. – TheDeadMedic Dec 7 '16 at 18:49
  • Can you post the code you're using to get the "Info Post" object in the first place? – TheDeadMedic Dec 7 '16 at 18:50
  • I'm using ACF but not pro, I don't really want to spend money doing a favor for a friend lol :D – av0000 Dec 7 '16 at 19:08
0

Ok, since we're going the post meta route (as opposed to options), there's a much cleaner and efficient way to grab a post by it's title:

if ( $info = get_page_by_title( 'Business Info', OBJECT, 'post' ) ) {
    // do stuff
}

Don't let the page in the function name throw you off - the third argument is the post type (in this case post).

Secondly, let's find out what's actually going on:

if ( $info = get_page_by_title( 'Business Info', OBJECT, 'post' ) ) {
    echo '<pre>';
    echo esc_html( print_r( get_post_meta( $info->ID ), true ) );
    echo '</pre>';
} else {
    echo 'Oh no!';
}

Report back with you what you get from the above debugging snippet.

  • Ok that worked I got back an array like this: Array ( [street_adress] => Array ( [0] => 123 Example Road ) ) – av0000 Dec 7 '16 at 19:24
  • I am still stuck on one thing though. I can get to the array but I can't use it. $meta = get_post_meta($info->ID); print_r($meta[street_address], true); print_r($meta['street_address'], true); Neither of these return anything, so I'm assuming I'm not using the array properly. – av0000 Dec 7 '16 at 19:42
  • And var_dump($meta["street_address"][0]); returns null, as done var_dump($meta->street_address); – av0000 Dec 7 '16 at 19:48
  • Nvm, had a typo and caught it. Thanks for the help! – av0000 Dec 7 '16 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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