Skip to content

Unable to fetch draft media by slug #3438

@opalenet-adrien

Description

@opalenet-adrien

Description

Hi!
My app needs to fetch media by slug on the add_attachment WP's hook.
When a media is uploaded in the library, graphql returns the media object.
But when the media is uploaded through the post editor (like for a featured image), graphql returns null.

Steps to reproduce

  1. Start to add a new post
  2. Click on "set featured image"
  3. Upload a new image

At this point, don't touch anything else, keep the editor open and the post as draft.

  1. Try to get the new media by its slug, null will be responded
  2. Try to get the same new media by its database id, you'll get the media object

As the media has been uploaded, its slug and database id can be found by editing it in the library.

Additional context

Here's an example of a query:

query NewQuery {
  mediaById: mediaItem(id: "7815", idType: DATABASE_ID) {
    id
    databaseId
    slug
    status
  }
  mediaBySlug: mediaItem(id: "3-2", idType: SLUG) {
    id
    databaseId
    slug
  }
}

And its response:

{
  "data": {
    "mediaById": {
      "id": "cG9zdDo3ODE1",
      "databaseId": 7815,
      "slug": "3-2",
      "status": "inherit"
    },
    "mediaBySlug": null
  },
  "extensions": {
    "debug": [],
    "queryAnalyzer": {
      "keys": "dee80d77ad26698c63b98a4b7eea631db104668cd4552723d5c2d666f2ca1150 graphql:Query operation:NewQuery cG9zdDo3ODE1",
      "keysLength": 110,
      "keysCount": 4,
      "skippedKeys": "",
      "skippedKeysSize": 0,
      "skippedKeysCount": 0,
      "skippedTypes": []
    }
  }
}

I understand that the media status is draft (inherited from the parent, the post still on draft). But if the query by database id can return the object, why can't the query by slug?
Please note that the requested object (id 7815) has the right requested slug (3-2).

This issue #1999 looks like having some similarities, but the use case is different. I'm trying to directly fetch the media, not the featuredImage through the post.

WPGraphQL Version

2.5.1

WordPress Version

6.8.3

PHP Version

8.4.14

Additional environment details

No WGraphQL extensions installed.
Also tried on PHP 8.3.15.

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have disabled ALL plugins except for WPGraphQL.

  • Yes
  • My issue is with compatibility with a specific WordPress plugin, and I have listed all my installed plugins (and version info) above.

Metadata

Metadata

Assignees

Labels

object type: mediaRelating to the MediaItem Typetype: bugIssue that causes incorrect or unexpected behavior

Type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions