• Hi,

    I’m a newbie in wordpress, finding it difficult to retrieve author name from custom get post meta api

    Below is snippet of my code

    add_action( 'rest_api_init', 'my_register_template_hook' );
    function my_register_template_hook() {
        register_rest_field( 'post', // any post type registered with API
            'appp',
            array(
                'get_callback'    => 'my_get_hook_data',
                'update_callback' => null,
                'schema'          => null,
            )
        );
    }
    function my_get_hook_data( $object, $field_name, $request ) {
        $data['post_detail']['below_title'] = '<span class="schedule-time">' . get_post_meta( $object['id'], 'author', true) . '</span>';
    
           $data['post_detail']['above_title'] = '<div class="post-featured-wrap">' . get_the_post_thumbnail( $object['id'], 'large', array( 'class' => 'post-featured' ) ) . '</div>'; 
           
       return $data;
    }

    I could only retrieve featured image

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Have you set a custom field named “author”? That is not how WP saves the post author by default, it only saves the user ID with posts (keyed as “author” or “post_author”). You then need to use something like get_userdata( $user_id ) to get the remaining author data.

    It’s not a great idea to save additional author data with post meta. Redundant data just increases the complexity of site maintenance. Ideally, no discrete information should appear in more than one place.

Viewing 1 replies (of 1 total)
  • The topic ‘How to add author name to custom get_post_meta’ is closed to new replies.