Support » Plugin: Jetpack by WordPress.com » JSON API Custom Post Types

  • Resolved thewirelessguy

    (@thewirelessguy)


    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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • reimerix

    (@reimerix)

    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 🙂

    thewirelessguy

    (@thewirelessguy)

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘JSON API Custom Post Types’ is closed to new replies.