Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#40600 closed enhancement (invalid)

get_item filter for hijacking single post request

Reported by: ruud@… Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:



I want to propose a filter in the get_item() method.

The use case is as follows;

  • Content from a WP REST API enabled repository is being shown on a site through a 'virtual' post mechanism.
  • The managers of the repository site use a custom way of setting whether or not a item is active, e.g via a metakey.
  • Once an end-user bookmarks this 'virtual' post URL, or if the 'virtual' post is accessed via menu's or internal links, it will try to retrieve the content from the repository.
  • If the managers of the repository site change the active property via its metakey value, the content should not come up anymore with a REST request.

The method I have in place right now has its own drawbacks, see: #40580, and from a performance standpoint it may not be very nice to first build the entire post object and then destroy in the end. Also using the rest_prepare_{$this->post_type} filter in this way isn't following its return policy as well (should be an WP_REST_Request not an WP_Error object)

With this filter one can now create a way for this to work.

I will add the patch and an unit test for this new filter.

Like to hear your thoughts.


Attachments (2)

40600.patch (1.1 KB) - added by ruud@… 4 years ago.
get_item() with rest_pre_get_item() filter
40600_unit_test.patch (1.2 KB) - added by ruud@… 4 years ago.
Unit test for get_item with filter

Download all attachments as: .zip

Change History (7)

4 years ago

get_item() with rest_pre_get_item() filter

4 years ago

Unit test for get_item with filter

#1 @ruud@…
4 years ago

  • Keywords has-patch has-unit-tests dev-feedback added

#2 @ruud@…
4 years ago


This may still be a valid addition to core but I managed to get the job done via extending the WP_REST_Posts_Controller class and using my own custom class to add the desired additional check in the get_item() method.

Use your own custom Controller via the post-type registration:

'rest_controller_class' => 'Custom_REST_Posts_Controller',


#3 @joehoyle
4 years ago

It seems this would be best serviced by just creating a subclass of WP_REST_Posts_Controller and using that subclass for the CTP via register_post_type. Any reason that route not be possible?

#4 @ruud@…
4 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Hi Joe,

Thanks for your feedback.
You are right, subclassing the post controller was entirely possible and solved my use case.

At first I was looking for a way to alter the default way it works via filters, therefor I came up with this proposal for this additional filter. In hindsight subclassing the post controller is a good way to solve this.

Unless you see more merits for this proposed filter, I would be OK with an 'wontfix' solution for this trac-ticket. Perhaps the documentation (or inline docs) could benefit some more on this topic.


#5 @ocean90
4 years ago

  • Keywords dev-feedback removed
  • Milestone Awaiting Review deleted
  • Type changed from feature request to enhancement
Note: See TracTickets for help on using tickets.