• Resolved diplatis

    (@diplatis)


    Is YARPP compatible with WP REST API?
    I want to get the related posts through the API.
    Thanks.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author YARPP

    (@jeffparker)

    @diplatis

    – What is your use case?
    – Is there a reason to want it via the WP Rest API specifically or would any API suffice?

    Thread Starter diplatis

    (@diplatis)

    I’m building a mobile app and want to get the related posts through WP API posts endpoint like Jetpack related posts do.

    related posts in JSON format would be great (wp-api)

    This would be a useful feature. I work on a React website that feeds data from the WordPress API.

    I created a plugin and figured out how to add related posts to the the REST API.

    function smi_get_related_posts_arguments() {
        $args = array();
    
        $args['postId'] = array(
    	'description' => esc_html('Post ID'),
    	'type' => 'integer',
    	'default' => '',
    	'validate_callback' => 'smi_validate_related_posts',
    	'sanitize_callback' => 'absint'
        );
    
        return $args;
    }
    
    function smi_validate_related_posts($value, $request, $param) {
    	$attributes = $request->get_attributes();
    
    	if (isset($attributes['args'][$param])) {
    	    $argument = $attributes['args'][$param];
    
    	    if (!empty($value)) {
    		if ('integer' === $argument['type'] && !is_int((int)$value)) {
    		    return new WP_Error('rest_invalid_param', sprintf(esc_html__( '%1$s is not of type Integer', 'smi'), $value ), array( 'status' => 400 ));
    		}
    
    		return true;
    	    }
    	}
    
    	return false;
    }
    
    function smi_get_related_posts( $request ) {
        $query_params = $request->get_params();
    
        if (empty($query_params['postId'])) {
        	return new WP_Error('smi', esc_html('A post ID is required', 'smi'), rest_ensure_response($query_params));
        }
        
        global $yarpp;
        return rest_ensure_response($yarpp->get_related($query_params['postId']));
    }
    
    function smi_register_routes() {
        $NAMESPACE = 'smi/v1';
    
        register_rest_route( $NAMESPACE, '/related-posts', array(
            array(
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'smi_get_related_posts',
                'args' => smi_get_related_posts_arguments()
            ),
        ));
    }
    
    add_action( 'rest_api_init', 'smi_register_routes' );

    It’s now available at the /wp-json/smi/v1/related-posts

    • This reply was modified 4 years, 8 months ago by xero2112.
    Plugin Author YARPP

    (@jeffparker)

    @xero2112 that’s great! We will plan to add it to the next release. May I invite you to our Github repo as a contributor so you can open a PR? If so, please share your GitHub username.

    @jeffparker Sure thing, my github username is JMV2112, thanks!

    Plugin Author YARPP

    (@jeffparker)

    @xero2112 invite sent! Please consider opening a Pull Request.

    • This reply was modified 4 years, 8 months ago by YARPP.

    Hi Sir,
    I have a wordpress site in my regional language. My requirement is when i add a new post it should come in android mob as a news in my language. When i expose wp rest api it is showing encoded data in posts.
    See url : https://34.216.119.12/sevasram/index.php/wp-json/wp/v2/posts/

    Please help me out of this.

    Plugin Author YARPP

    (@jeffparker)

    @mailstokrishnan can you please provide an example here? The URL you included is leading to a 404 page currently.

    Plugin Author YARPP

    (@jeffparker)

    @xero2112 left feedback on your PR. Would appreciate it if you can take a look at it.

    Plugin Support Michael Nelson

    (@mnelson4)

    FYI YARPP 5.3 adds a REST API endpoint, documented here: https://support.shareaholic.com/hc/en-us/articles/360046456752-YARPP-REST-API-for-Related-Posts
    It’s pretty basic so far, but we tried to model it after the WP posts route as much as possible. Feedback welcome!

    I’m tentatively closing this. Feel free to reopen, but also consider creating new issues.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘WP REST API compatibility’ is closed to new replies.