• Resolved laportjo

    (@laportjo)


    Hi,

    Is there an easy way to render custom post type ‘idea’ meta_box number of votes with the REST API ?
    I alreader add this line in the ideapush.php file to be able to see each CPT with the rest API
    'show_in_rest' => true,

    But the data manager here ask me how to access the number of votes with the API and it doesn’t seem to show up.

    Thanks!

    Jonathan

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter laportjo

    (@laportjo)

    Ok I found the solution. I put it in my theme functions file :

    function add_votes_field() {
    register_rest_field(
    'idea', 
    'votes', //Field Name in JSON RESPONSE
    array(
            'get_callback'    => function() {
            return get_post_meta( get_the_ID(), 'votes', true );
            }
        )
    );
    }
    add_action( 'rest_api_init', 'add_votes_field' );
    
    function idea_push_update_custom_post_type( $args, $post_type ) {
        if ( $post_type !== 'idea' ) {
             return $args;
        }
    
        // Add additional IDEA CPT options.
        $idea_args = array(
            'show_in_rest' => true,
        );
     // Merge args together.
     return array_merge( $args, $idea_args );
    }
    add_filter( 'register_post_type_args', 'idea_push_update_custom_post_type', 10, 2 );
    Plugin Author Northern Beaches Websites

    (@northernbeacheswebsites)

    Hi @laportjo,

    What you did there by getting the data from the post meta is exactly right. You did well to figure that out, good job! Sorry I couldn’t help earlier I was asleep ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Rest API meta box number of votes’ is closed to new replies.