Category: WordPress

  • How to Delete Meta Fields with the WordPress REST API

    As of this writing, it is not possible to delete a meta value using the WordPress REST API. Post meta updates must be communicated while inserting and updating post objects, and the only way to “remove” meta fields is to write blank values over their current values.

    I wrote a free and open-source plugin to provide granular control over post and term meta fields. It lives on Github. The plugin is called Manipulate Meta with the WP API, and it extends the REST API to allow one request to retrieve, update, or delete a meta field independently from a post or term object.

    The instructions are located in the repo’s README.md.

  • Hiding Specific Terms When Creating or Editing Posts

    The WordPress block editor uses the REST API to manipulate all information in the post object, so hiding terms from the category, tags, or a custom taxonomy meta box can be achieved by removing those terms from REST API responses.

    One hook that makes this easy is the rest_{taxonomy}_collection_params filter.

    Here is an example that shows how terms can be included in the editor only if they have a certain value for a term meta key. This isn’t the best code design because it uses a global variable to hold the taxonomy name, but that’s an artifact of my pulling this out of a larger class.

    For my application, the terms I’m excluding are outdated and won’t be added to any new content on the site. This method is effective and seems low risk. If you are aware of any risks associated with excluding terms from REST API responses, please comment below.