1

In gutenberg I want to get a postmeta (it's a json string) that belongs to post #123. I want to check the editor's content against it. Let's say that postmeta's key is "blocks".

So I made this

(function () {
    if (true === window.location.pathname.includes('post.php')) {
        const unsubscribe = window.wp.data.subscribe(function () {
            const arr = JSON.parse('{"first":"1","second":"2"}');

            for (const key in arr) {
                console.log(
                    wp.data
                        .select('core/editor')
                        .getEditedPostContent()
                        .includes(arr[key])
                );
            }
        });
    }
})();

Which works, I can get the content, but I want to replace arr with the json that's in postmetas. Is there a way I can do this with wp.data? I can get current post's metas, but I want post #123's metas.

1 Answer 1

0

Here’s a way that could be “best” depending on the circumstance:

const { meta } = wp.data.select( 'core' ).getEntityRecord( 'postType', 'post', 123 );

This tutorial might be helpful for related stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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