• Hi,

    I was in need to fetch data using page link/url that is available to me. Can we call wordpress rest api using link? If yes, may I know how can that be done? Shall appreciate responses.
    Thanks

Viewing 15 replies - 1 through 15 (of 16 total)
  • You can link to a REST API endpoint, but you’re just going to get a JSON response.

    What are you actually trying to accomplish?

    • This reply was modified 8 years, 9 months ago by Jacob Peattie.
    Thread Starter tejasbramhecha

    (@tejasbramhecha)

    Hello @jakept,

    What I’m trying here is to get json data from wordpress but to call wp rest api, I need something like tag/category/id, right? but all that I have is link/url so I am just asking if I can call wp rest api using that link which is available to me & get json data? If yes, how

    No, you can’t. If you have the *slug* of the category, you would request categories with that slug using the categories endpoint:

    /wp/v2/categories?slug=category-slug-you-want-id-for
    

    And then get the ID of the category out of that response, then use the posts endpoint to get posts with that category:

    /wp/v2/posts?categories=category-id-that-you-found
    
    • This reply was modified 8 years, 9 months ago by Jacob Peattie.
    Thread Starter tejasbramhecha

    (@tejasbramhecha)

    Hi @jakept,

    May I know if we can retrieve posts using title?
    e.g. /wp/v2/posts?title=Testing Cloud Security

    or is there any way to do this?
    Thanks

    @tejasbramhecha You can see the ways you can retrieve pages with the API in the documentation: https://developer.ww.wp.xz.cn/rest-api/reference/pages/#list-pages

    Title isn’t one of them. You could use search to search for a page by title, but you couldn’t 100% guarantee it’s the page you want.

    Thread Starter tejasbramhecha

    (@tejasbramhecha)

    Hi @jakept,

    I tried the solution that you gave to me. I tried to get posts using following api call: /wp/v2/posts?categories=category-id-that-I-found but this returned me something like this “Unexpected ‘<‘”. Would please let me know what’s wrong here?

    Thanks

    That error just means there’s a syntax error somewhere in your code. It would have given you a line number in your code to look at. Go to it and check for typos.

    Thread Starter tejasbramhecha

    (@tejasbramhecha)

    Hi @jakept,

    I’m running this directly on postman. There’s no code. The simple aim that I have here is to retrieve posts that have this category id. I have checked my categories too to ensure that the id I’m using is there in dataset.

    Doesn’t change the fact that “Unexpected …” is a syntax error. There’s still code on the WordPress site you’re requesting from, so the issue must be there.

    Thread Starter tejasbramhecha

    (@tejasbramhecha)

    Hi @jakept,

    I could fetch posts by using category id now. But for some ids it shows null to me even when there is data in backend with that category id. Would you please let me know why is it happening so?

    If you’re getting null it’s because there aren’t any posts in that category. I can’t help any more than that without a URL.

    EDIT: Put it this way:
    http://website.com/wp-json/wp/v2/posts?categories=1
    Should return the same posts as
    http://website.com/?cat=1
    (But in JSON, obviously)

    So make sure you’re also not getting any posts with the second URL.

    Thread Starter tejasbramhecha

    (@tejasbramhecha)

    Hi @jakept,

    I want to retrieve portfolio data from my website. Is there any way that I can make a call to wp-rest-api & get it? If yes, can you please let me know how? If no, can we add custom post type as portfolio in wp-rest-api code & make it work? Unfortunately, I’m unable to find proper documentation regarding custom post types. Shall be grateful if you can help somehow.
    Thanks

    Look at the documentation for register_post_type(), particularly the properties for show_in_rest and rest_base. If the post type has custom fields you need to access, look at register_meta().

    Thread Starter tejasbramhecha

    (@tejasbramhecha)

    Hi @jakept,

    I’m now able to access portfolio data using wp rest api but the json that I’m getting is not having any category id/tag mentioned in it. Will you please let me know what can be done to make sure that I get category id/tag?

    I have written code as following:

    
    /**
     * Add REST API support to an already registered post type.
     */
    add_action( 'init', 'my_custom_post_type_rest_support', 25 );
    function my_custom_post_type_rest_support() {
      global $wp_post_types;
     
      //be sure to set this to the name of your post type!
      $post_type_name = 'startapp_portfolio';
      if( isset( $wp_post_types[ $post_type_name ] ) ) {
        $wp_post_types[$post_type_name]->show_in_rest = true;
        $wp_post_types[$post_type_name]->rest_base = $post_type_name;
        $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
        
      }
    }
    

    Thanks,

    Taxonomies also need to be set to show in REST: https://developer.ww.wp.xz.cn/reference/functions/register_taxonomy/

Viewing 15 replies - 1 through 15 (of 16 total)

The topic ‘Call WordPress rest api using links/url’ is closed to new replies.