0

I created a new meta box for a custom post type, but I cannot get the data (the user's city) to show up under Location.

enter image description here

Here is my code:

function wporg_add_custom_box()
{
        add_meta_box(
            'wporg_box_id',           // Unique ID
            'Location',               // Box title
            'custom_meta_box_markup',  // Content callback, must be of type callable
            'project',                // Post Type
            'side', 
            'core');
}
add_action('add_meta_boxes', 'wporg_add_custom_box');

function custom_meta_box_markup() {
    global $post;
    $custom_fields = get_the_author_meta( 'city', $author_id );
    ?>
        <div>
            <input name="custom_fields" type="text" value="<?php echo $custom_fields;?>">
        </div>
<?php }
edit7279
  • 175
  • 5
  • 15

2 Answers2

0
syntax: get_the_author_meta( string $field = '', int $user_id = false )

Codex says: Return:(string) The author's field from the current author's DB object, otherwise an empty string.

I believe your function are returning empty results.

Try:

get_the_author_meta( 'city', get_current_user_id() );

if you are editing you may use $post->author_id instead get_current_user_id()

caiovisk
  • 3,667
  • 1
  • 12
  • 18
  • get_the_author_meta( 'city', get_current_user_id() ); renders my location as the admin, not the users/authors location --- I tried $post->author_id too, but that didn't render any data – edit7279 Feb 12 '18 at 05:54
  • You can debug the result from get_the_author_meta() by using `var_dump`, also check if your user author have all the info entered, may the city field is empty. – caiovisk Feb 12 '18 at 06:06
  • Not sure how to do var_dump... The author is a fake user I created. I have managed to render the users location/city in other admin areas of the site, just can't get it on the edit post page. – edit7279 Feb 12 '18 at 06:12
  • So, if it is working within the admin, it must work within the $post author. Try to debug it, do a `var_dump($post)` right after `global $post`, and see if the right author and/or post is returning... Also see: https://stackoverflow.com/questions/14743342/how-do-i-properly-use-print-r-or-var-dump – caiovisk Feb 12 '18 at 06:20
  • var_dump shows the correct author -- 16 is the fake user's ID ["post_author"]=> string(2) "16" – edit7279 Feb 12 '18 at 06:45
  • Got it. $custom_fields = get_the_author_meta( 'city', $author_id=$post->post_author ); – edit7279 Feb 12 '18 at 06:59
0

SOLUTION:

$custom_fields = get_the_author_meta( 'city', $author_id=$post->post_author );

edit7279
  • 175
  • 5
  • 15