0

I'm really struggling trying to get any information at all on how to do this. the project is part of a church website holding a collection of 'talks' (custom post type) which has data attached such as AudioURL, VideoURL, Speaker, Series etc. Speaker and Series are custom taxonomies rather than free text.

The aim is to allow someone who doesn't understand the structure to be able to add posts, and so all required data is entered via a custom block on the edit screen. Much of this has been acheived whether stored as Wordpress data (e.g. Title, slug) or custom fields, and I can get the complete list of taxonomy terms to populate a SelectControl of ComboboxControl.

What is missing is being able to get the current value of the post's taxonomy term, and being able to set it. I'm pretty sure I need to use GetEntityRecords and EditEntityRecords, but I can't find any useful documentation or examples.

Can anyone tell me how it is done?

For setting it, I have tried

function updateSeriesValue( newValue ) {
            wp.data.dispatch( 'core' ).editEntityRecord( 'post_type', 'talks', post_id, { 'series': [ newValue ] } );
        };

which gives me the error: Uncaught (in promise) Error: The entity being edited (post_type, talks) does not have a loaded config.

For reading the value, I've tried

wp.data.select('core').getEntityRecords('post_type','talks',post_id,'series');

but I'm pretty sure that is wrong.

Thanks in advance

4
  • Is the taxonomy you're trying to work with set to show in the REST API? My understanding is that editEntityRecord edits the original entity, so your first attempt does not set the working copy of the posts terms, but rather it attempts to edit the term itself directly on the website via the API.
    – Tom J Nowell
    Mar 15 at 17:48
  • Yes, all taxonomies and custom post types are set to show in the REST API. Mar 16 at 10:25
  • Is there a reason you don't use getEditedPostAttribute or editPost? Modifying entity records isn't the same as checking the term box in the editor UI, it's the same as editing the term on the category admin page. The solution should be very very similar to what the tutorials about setting post meta look like
    – Tom J Nowell
    Mar 16 at 10:33
  • @tom I haven't come across either of those - I'll look them up. I haven't found much information, or places to get information, on how to do things in block editor code so any pointers to references would be welcome. I didn't really know any Javascript until I starting learning about custom blocks in the last couple of months so trying to look at source code often doesn't help. Mar 16 at 19:06
0

I gave up and went for a php solution. The code in the custom block uses and updates a meta field, then I use the 'updated_post_meta' hook to set the taxonomy term from the meta field. Before editing I use the 'the_post' hook to set the meta value from the taxonomy term.

A bit clunky, but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.