WordPress.org

Forums

Jetpack by WordPress.com
[resolved] JSON API Custom Post Types (3 posts)

  1. thewirelessguy
    Member
    Posted 1 year ago #

    I'm trying to get custom post types to work with the Jetpack JSON module. I've added the following to my functions.php to whitelist my custom post type 'portfolio':

    add_filter( 'rest_api_allowed_post_types', array($this, 'allow_my_post_types' ) );

    function allow_my_post_types($allowed_post_types) {
    if (! defined('REST_API_REQUEST') || ! REST_API_REQUEST)
    return $allowed_post_types;
    $allowed_post_types[] = 'portfolio';
    return $allowed_post_types;
    }

    Unfortunately I'm still getting this error:
    {"error": "unknown_post_type","message": "Unknown post type"}
    I'm using https://public-api.wordpress.com/rest/v1/sites/www.thewirelessguy.co.uk/posts/?type=portfolio to call the JSON.

    Any ideas?

    http://wordpress.org/extend/plugins/jetpack/

  2. reimerix
    Member
    Posted 1 year ago #

    Okay, i actually don't really have a clue what I did here, but this fixed it for me:

    function allow_my_post_types($allowed_post_types) {
    $allowed_post_types[] = 'portfolio';
    return $allowed_post_types;
    }
    
    add_filter( 'rest_api_allowed_post_types', 'allow_my_post_types');

    Thanks for providing your example in the first place :)

  3. thewirelessguy
    Member
    Posted 1 year ago #

    A huge thanks to you. The problem seems to have been with the array($this,''). I tried adding back in the other parts of the function with just the changes you made to add_filter and it still worked. The function I wrote was based on the bbpress example they included in the plugin.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic