1

Hope you are having a great day!

I am developing a theme and in my functions.php, I have registered two custom post types—modules and sessions—like this:

    register_post_type( 'module', [
        'labels' => [
            'name' => 'Modules',
            'singular_name' => 'Module',
            'add_new' => 'Add New Module',
            'add_new_item' => 'Add New Module',
            'edit_item' => 'Edit Module',
            'new_item' => 'New Module',
            'view_item' => 'View Module',
            'view_items' => 'View Modules',
            'search_items' => 'Search Modules',
            'not_found' => 'No modules found',
            'not_found_in_trash' => 'No modules found in trash',
            'all_items' => 'All Modules',
            'archives' => 'Module Archives',
            'attributes' => 'Module Attributes',
            'insert_into_item' => 'Insert into module',
            'uploaded_to_this_item' => 'Uploaded to this module',
            'filter_items_list' => 'Filter modules list',
            'items_list_navigation' => 'Modules list navigation',
            'items_list' => 'Modules list',
            'item_published' => 'Module published',
            'item_published_privately' => 'Module published privately',
            'item_reverted_to_draft' => 'Module reverted to draft',
            'item_trashed' => 'Module trashed',
            'item_scheduled' => 'Module scheduled',
            'item_updated' => 'Module updated',
            'item_link' => 'Module Link',
            'item_link_description' => 'A link to a module',
          ],
        'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( get_template_directory() . '/assets/icons/module.svg' ) ),
        'menu_position' => 20,
        'public' => true,
        'supports' => [ 'title', 'thumbnail', 'excerpt' ],
        'show_in_rest' => true,
        'rest_base' => 'modules',
      ] );
    register_post_type( 'session', [
        'labels' => [
            'name' => 'Sessions',
            'singular_name' => 'Session',
            'add_new' => 'Add New Session',
            'add_new_item' => 'Add a New Session',
            'edit_item' => 'Edit Session',
            'new_item' => 'New Session',
            'view_item' => 'View Session',
            'view_items' => 'View Sessions',
            'search_items' => 'Search Sessions',
            'not_found' => 'No sessions found',
            'not_found_in_trash' => 'No sessions found in trash',
            'all_items' => 'All Sessions',
            'archives' => 'Session Archives',
            'attributes' => 'Session Attributes',
            'insert_into_item' => 'Insert into session',
            'uploaded_to_this_item' => 'Uploaded to this session',
            'filter_items_list' => 'Filter sessions list',
            'items_list_navigation' => 'Sessions list navigation',
            'items_list' => 'Sessions list',
            'item_published' => 'Session published',
            'item_published_privately' => 'Session published privately',
            'item_reverted_to_draft' => 'Session reverted to draft',
            'item_trashed' => 'Session trashed',
            'item_scheduled' => 'Session scheduled',
            'item_updated' => 'Session updated',
            'item_link' => 'Session Link',
            'item_link_description' => 'A link to a session',
          ],
        'menu_icon' => 'dashicons-microphone',
        'menu_position' => 20,
        'public' => true,
        'supports' => [ 'title', 'editor', 'custom-fields' ],
        'show_in_rest' => true,
        'rest_base' => 'sessions',
      ] );

Modules and sessions can then be requested with the REST API by using requests like:

  • https://<site-domain>/wp-json/wp/v2/modules?_embed=wp:featuredmedia,wp:term&_fields=id,slug,date_gmt,title.rendered,_links,_embedded
  • https://<site-domain>/wp-json/wp/v2/sessions?_embed=wp:featuredmedia,wp:term&_fields=id,slug,date_gmt,title.rendered,_links,_embedded

My problem lies in the fields _links and _embedded of the returned JSON which, for instance, look like this for a post that supports featured images:

"_links":{
    ...
    "wp:featuredmedia":[
       {
          "embeddable":true,
          "href":"XXX"
       }
    ],
    ...
},
"_embedded":{
   "wp:featuredmedia":[
      {
        ...
      }
    ],
    ...

A module is a collection of 0..* sessions and a session can only be part of one module. I need to make this one-to-many relation available with the REST API through the _links and _embedded fields. The request should return a JSON looking like this for a particular module:

"_links":{
    ...
    "custom:sessions":[
       {
          "embeddable":true,
          "href":"XXX"
       }
    ],
    ...
},
"_embedded":{
   "custom:sessions":[
      {
        ...
      }
    ],
    ...

Is it possible to do that?

3
  • 1
    If it's not too late, may I suggest, just as a rule of thumb, that you move these custom post types to a plugin rather than a theme. That way if you ever switch the theme these post types don't disappear. Commented Jul 23 at 17:08
  • I agree with Tony Djukic comment because a theme aim is to manage public pages and not to manage datas.
    – mmm
    Commented Jul 23 at 18:33
  • to customise the api output, look at this filter : developer.wordpress.org/reference/hooks/…
    – mmm
    Commented Jul 23 at 18:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.