WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 5 weeks ago

#39466 closed defect (bug) (fixed)

Get list of post API request missing status

Reported by: gnanasekaran Owned by: jnylen0
Milestone: 4.7.3 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests fixed-major
Focuses: rest-api Cc:

Description (last modified by jnylen0)

when send "get posts" request, status ($data['status']) is missing by default. I did not get "status" in response.

For below request

Definition
GET /wp/v2/posts/
Example Request

$ curl http://demo.wp-api.org/wp-json/wp/v2/posts/

Attachments (4)

39466.patch (718 bytes) - added by dhanendran 6 weeks ago.
Fix for this ticket
39466-1.patch (1.4 KB) - added by dhanendran 5 weeks ago.
Fix for this ticket along with unit tests
39466.2.diff (1.6 KB) - added by jnylen0 5 weeks ago.
Improved unit tests
39466.3.diff (3.3 KB) - added by rachelbaker 5 weeks ago.
Includes an updated tests/qunit/fixtures/wp-api-generated.js file

Download all attachments as: .zip

Change History (24)

#1 @kadamwhite
3 months ago

I'm on the go and haven't perused the code so this is just a shot in the dark, but most statuses require authentication to access -- it's possible that the status you're requesting is being overwritten because only "publish" is available to unauthenticated users. I'd suggest making the request with an authenticated JavaScript client from within the admin, or installing one of the authentication plugins if you want to use curl, and seeing if that resolves the issue; my hunch is that this is a docs problem more than it is a code problem.

#2 follow-up: @jnylen0
3 months ago

  • Description modified (diff)
  • Keywords needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to 4.7.2

We do only show the status in context=edit (ref).

This has been reported at least one other time in testing. I think it's a bit silly not to include status: we support querying for a post status unauthenticated, even if just for the default status of publish (ref) or inherit for attachments (ref).

Another status-related issue: It's not possible to query posts with the inherit status unauthenticated. But, it looks like they may show up in /wp/v2/posts if read permission is otherwise allowed. Needs some testing.

#3 in reply to: ↑ 2 @rmccue
3 months ago

Replying to jnylen0:

I think it's a bit silly not to include status: we support querying for a post status unauthenticated, even if just for the default status of publish (ref) or inherit for attachments (ref).

FWIW, this has been the case since this commit. Not sure why we made the decision then, but it does somewhat make sense to add this to the view context, if only to distinguish between publish and inherit (although inherit isn't actually allowed in WP on anything except attachment, to my knowledge).

This ticket was mentioned in Slack in #core by jnylen. View the logs.


2 months ago

#5 @jnylen0
7 weeks ago

  • Keywords good-first-bug added

This ticket was mentioned in Slack in #forums by jnylen. View the logs.


7 weeks ago

#7 @dhanendran
7 weeks ago

@rachelbaker committed this https://github.com/WP-API/WP-API/commit/69f617d749e4f165b9e31c1c042eec45ade3e41f one. Can we get some suggestion from @rachelbaker before deciding something?

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


6 weeks ago

#9 @rachelbaker
6 weeks ago

@dhanendran I honestly don't remember the logic for moving status to the edit context. Only one that comes to mind is that the status/visibility of a resource is rarely exposed publically.
However, in v1 of the REST API we did expose post_status for all contexts: https://github.com/WP-API/WP-API/blob/master/lib/class-wp-json-posts.php#L556

I do not have a strong opinion here one way or another.

#10 follow-up: @jnylen0
6 weeks ago

Status has a small set of possible values that can be filtered publicly, so this parameter is effectively already public (exposing it provides no new information that can't already be obtained elsewhere).

#11 in reply to: ↑ 10 @dhanendran
6 weeks ago

@rachelbaker @jnylen0 Thanks for your input.

Status has a small set of possible values that can be filtered publicly, so this parameter is effectively already public (exposing it provides no new information that can't already be obtained elsewhere).

Yes I do agree with this.

@dhanendran
6 weeks ago

Fix for this ticket

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


6 weeks ago

#13 @jnylen0
6 weeks ago

  • Keywords has-patch added; needs-patch removed
  • Owner set to jnylen0
  • Status changed from new to accepted
  • Version changed from 4.6.1 to 4.7

@dhanendran: can you update the test suite for this change, including a new test if needed? See: https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/

This ticket was mentioned in Slack in #core-restapi by adamsilverstein. View the logs.


6 weeks ago

@dhanendran
5 weeks ago

Fix for this ticket along with unit tests

#15 @dhanendran
5 weeks ago

  • Keywords has-unit-tests added; needs-unit-tests removed

@jnylen0 I have added the unit test case for this fix.

@jnylen0
5 weeks ago

Improved unit tests

#16 @jnylen0
5 weeks ago

@dhanendran I'd prefer to test the actual response coming from the API endpoint, as in 39466.2.diff.

I think this is good to go for 4.7.3. I'll wait a couple more days before committing to give others a chance to review.

@rachelbaker
5 weeks ago

Includes an updated tests/qunit/fixtures/wp-api-generated.js file

#17 @rachelbaker
5 weeks ago

  • Keywords commit added; good-first-bug removed

@jnylen0 Your 39466.2.diff looked good, just was missing an updated schema file for the qunit tests. I added this in 39466.3.diff.

#18 @rachelbaker
5 weeks ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 40080:

REST API: Include the status property in view context responses from the Posts endpoints.

Previously the status for a Post (or other post_types) was only exposed under the edit context, which doesn't really make much sense considering we support querying by post status without authentication. Originally introduced in v2.0 beta 1: https://github.com/WP-API/WP-API/commit/69f617d749e4f165b9e31c1c042eec45ade3e41f without any explanation in the commit message.

Props dhanendran, jnylen0, rachelbaker.
Fixes #39466.

#19 @rachelbaker
5 weeks ago

  • Keywords fixed-major added; commit removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

Re-opening for backporting to the 4.7 branch for 4.7.3.

#20 @rachelbaker
5 weeks ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 40081:

REST API: Include the status property in view context responses from the Posts endpoints.

Previously the status for a Post (or other post_types) was only exposed under the edit context, which doesn't really make much sense considering we support querying by post status without authentication. Originally introduced in v2.0 beta 1: https://github.com/WP-API/WP-API/commit/69f617d749e4f165b9e31c1c042eec45ade3e41f without any explanation in the commit message.

Props dhanendran, jnylen0, rachelbaker.
Merges [40080] to the 4.7 branch.
Fixes #39466.

Note: See TracTickets for help on using tickets.