• Resolved priyankac

    (@priyankac)


    Hello Team,

    We are using a web story plugin and created a custom rest API and fetched all web stories from this custom API.

    Please find the below function what we are used :

    // displayed all webstories using api
    
    add_action( 'rest_api_init', 'get_all_webstory_posts' );   
    
    function get_all_webstory_posts() {
        register_rest_route( 'wp/v2', '/web-story-list', array(
            'methods' => 'GET',
            'callback' => 'get_all_webstories_callback'
        ));
    }
    
    function get_all_webstories_callback( $request ) {
        // Initialize the array that will receive the posts' data. 
        $posts_data = array();
        // Receive and set the page parameter from the $request for pagination purposes
        $paged = $request->get_param( 'page' );
        $paged = ( isset( $paged ) || ! ( empty( $paged ) ) ) ? $paged : 1; 
        // Get the posts using the 'post' and 'news' post types
        $posts = get_posts( array(
                'paged' => $paged,
                'posts_per_page' => -1,            
                'post_type' => array( 'web-story' ),
            )
        ); 
        // Loop through the posts and push the desired data to the array we've initialized earlier in the form of an object
        foreach( $posts as $post ) {
            $id = $post->ID; 
            $post_thumbnail = ( has_post_thumbnail( $id ) ) ? get_the_post_thumbnail_url( $id ) : null;
    
            $posts_data[] = (object) array( 
                'id' => $id, 
                'slug' => $post->post_name, 
                'type' => $post->post_type,
                'title' => $post->post_title,
                'featured_img_src' => $post_thumbnail,
    			'permalink' => get_permalink( $id ),
                'published_date' => get_the_time('M d,Y', $id),
                 'cat' => get_categories(),
    			
            );
        }                  
        return $posts_data;                  
    }

    In above mentioned code we got result id,slug,title,featured_img_src, and published date but we don’t get category name.

    So our query is :
    1. We have categories and this category assigned some posts also we are trying to fetch category names on our API it’s not getting. Can You please guide us on how to fetch categories names on API.
    3. Is there any different name on fetching categories EX: web_story_category

    Please let me know how to fetch categories name on API

    Thank You

Viewing 1 replies (of 1 total)
  • Plugin Author Pascal Birchler

    (@swissspidy)

    get_categories() returns a list of category objects from the default category taxonomy in WordPress.

    In the Web Stories plugin we do not use the category taxonomy, but a custom one with the slug web_story_category.

    get_categories( array( 'taxonomy' => 'web_story_category' ) ) should do the trick for you.

    Please note that we don’t usually offer support for custom coding questions like this though ??

    I recommend checking the WordPress REST API reference documentation, especially around linking and embedding additional information such as taxonomy data.

Viewing 1 replies (of 1 total)
  • The topic ‘How to fetch category name in web story custom api’ is closed to new replies.