3

I'm answering myself for the benefit of others. I was passing an array of post type slugs to apiFetch instead of passing an array with their rest_base parameter. So, the correct function should be: const postTypes = useSelect( ( select ) => { const { getPostTypes } = select( 'core' ); const excludedPostTypes = [ 'attachment' ]; const ...


2

Short Answer The second parameter for useSelect is the second parameter for useCallback, so check the React website and you'd understand what the heck that second parameter is.. :) Long Answer useSelect is a custom React hook which uses the core useCallback hook in React which returns a memoized callback which can avoid expensive calculations on every render,...


2

This took me a while to figure out... It's a bug. The JavaScript error occurs because the onFilterValueChange function is not defined... Even though the WordPress documentation says this function is optional. Simply adding onFilterValueChange with an anonymous function removes the JavaScript error. E.g. <ComboboxControl label="Font Size" ...


2

My question is, is there a similar selector for when you don't know the post type of a post beforehand? No, at the current time of writing there is not. The fundamental problem is that the REST API doesn't provide a generic mechanism for getting a post type given a post ID. You can retrieve a post and it will say post but to do this you need to know the ...


2

Here's a solution to your question. This doesn't take into account the merit of whether or not you should add async or defer to certain assets, it just gives you the tools to do it yourself, or not. Points to note: this is only for use with Javascript includes (obviously) It's not 100% accurate because of the method of replacing the tag text, but for 99.9% ...


1

i don't know with version you use, but if you wan't load stuff on the initialisation of js you can have it on file using js : ( function( wp ) { your stuff } )( window.wp ); if you really want inline use in php ; https://developer.wordpress.org/reference/functions/wp_add_inline_script/ wp_add_inline_script( 'name_of_the_script_Woocommerce', <script> ...


1

Instead of doing multiple apiFetch calls for each post type, a more optimal solution to make a search across multiple post types is to use the WP REST API search endpoint, which I didn't know it existed until now. The downside is that you don't get the full post objects using this endpoint, but if you only need the post id, title, url and type, it's ok: ...


1

I'm getting a Promise with the post object inside when I do a console.log( mypost ) Yes, because apiFetch() indeed returns a Promise object, and apiFetch() doesn't assign the response received from the server to the mypost constant, and the return value of your then() callback is also not going to be assigned to the mypost variable. How can I get the post ...


1

When you do this: '_related_posts': [post.id], you're actually overriding the entire array or the meta value, so to instead append an item to the existing array, you can use the spread (...) syntax like so: '_related_posts': [ ...meta._related_posts, post.id ]. But you may want not want to update the meta if the selected post ID is already in the current ...


Only top voted, non community-wiki answers of a minimum length are eligible