Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP 5.6 getCurrentPostType and others not working anymore #28225

Closed
Elkex opened this issue Jan 15, 2021 · 4 comments
Closed

WP 5.6 getCurrentPostType and others not working anymore #28225

Elkex opened this issue Jan 15, 2021 · 4 comments

Comments

@Elkex
Copy link

Elkex commented Jan 15, 2021

Hello!

After updating to WordPress 5.6, we noticed that getting the getCurrentPostType, getCurrentPostId,... API functions are not working anymore.

wp.domReady( () => {
	const postType = select( 'core/editor' ).getCurrentPostType();
});

I cannot seem to find an alternative or updated version of this hook.

Anyone an idea?

Thanks in advance!

@ntsekouras
Copy link
Contributor

Hey @Elkex 👋 - thanks for reporting this!

Actually the API works fine. The problem lies in wp.domReady.

I'll close this one in favor of:#25330, that has a bit more context.

@nickpish
Copy link

Would someone be able to provide a full, working example of how to use getCurrentPostType()? I've read through all the linked threads relating to this issue, and I'm still not able to get wp.data.select( 'core/editor' ).getCurrentPostType() to return anything other than null. I'm trying to get the current post type to allow for conditionally unregistering blocks and modifying settings, using a script enqueued via enqueue_block_editor_assets. I've tried adding numerous dependencies, and nothing seems to work. This thread comes up high in Google searches for "getCurrentPostType not working" so I'd imagine others might find an example of a working solution helpful as well- thanks!

@MarkMarzeottiDI
Copy link

@nickpish Came across this today and pieced together an answer to the question as I had it as well. As mentioned above wp.data.select( 'core/editor' ).getCurrentPostType() does not seem to be available at wp.domReady. The solution is described in issue #25330 by user markhowellsmead. I was able to use subscribe to wait until the post type becomes available, do what I need to with that info, then unsubscribe. My code where I am removing a taxonomy panel if the user is editing a 'location' post looks like this:

const getPostType = () => wp.data.select('core/editor').getCurrentPostType();
let postType = null;

const unsubscribe = wp.data.subscribe(() => {
	const newPostType = getPostType();
	if (newPostType !== postType) {
		unsubscribe();
		postType = newPostType;
		if ('location' === postType) {
			wp.data
				.dispatch('core/edit-post')
				.removeEditorPanel('taxonomy-panel-associated_location');
		}
	}
});

I'm not certain this is the most efficient way, but since wp.domReady was being fired before all the data was available, this is working to run the code as soon as it becomes available.

@nickpish
Copy link

nickpish commented Apr 3, 2024

Thank you, @MarkMarzeottiDI - I will give this a shot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants