wp-graphql icon indicating copy to clipboard operation
wp-graphql copied to clipboard

Custom Post Types label returns null outside of WordPress.

Open rbourassaca opened this issue 5 years ago • 1 comments

WPGraphQL

Hi, I tried to start over a few times on different installations, but I always end up with the same problem. I currently need to retrieve the label from my custom post types. When I do inside GraphiQL in Wordpress everything is fine, but, if I request it outside of WordPress it returns null.

I am currently running WordPress 5.5.3, WP GraphQL 1.0 and WP Gatsby 0.7.2.

Here's how I created my custom post types:

    $labels = [
        "name" => __( "Vidéos" ),
        "singular_name" => __( "Vidéo" ),
    ];
$args = [
    "label" => __( "Vidéos" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => [ "slug" => "video", "with_front" => true ],
    "query_var" => true,
    "menu_icon" => "dashicons-format-video",
    "supports" => [ "title", "thumbnail", "custom-fields" ],
    'show_in_graphql' => true,
'graphql_single_name' => 'video',
'graphql_plural_name' => 'videos',
];

register_post_type( "video", $args );

I searched for a week and I did not find anything that works to resolve that issue.

Thanks for this awesome project!

rbourassaca avatar Dec 07 '20 18:12 rbourassaca

@rbourassaca Thanks for the kind words. Glad that you're enjoying WPGraphQL! 😄

Some fields in the Graph are not considered public by default. The labels of Post Types are only exposed within the WordPress admin, and WPGraphQL follows suit here, providing the data to logged in users but not public users.

I wrote up a quick snippet to show how to filter the WPGraphQL Model layer and allow the "label" field to be exposed publicly.

https://www.wpgraphql.com/recipes/showing-post-type-labels-in-public-queries/

You can follow the example to expose other fields as needed.

Just make sure you're careful exposing information publicly that is otherwise considered restricted to admins by WordPress core.

jasonbahl avatar Dec 08 '20 13:12 jasonbahl