evermade / wp-block-toolkit

Toolkit for developing WordPress Gutenberg blocks.
https://www.evermade.fi
GNU General Public License v3.0
9 stars 1 forks source link

PostControl: Show only posts created by the current user #2

Closed mipon closed 2 years ago

mipon commented 2 years ago

Hi,

Nice work.

Is it possible to limit the post list options of PostControl to posts created by the currently editing user?

tnke commented 2 years ago

@mipon You should be able to achieve that by passing only the current user's posts to PostControl like this:

const postType = "post";

const { id: authorId } = useSelect((select) => {
     return select("core").getCurrentUser();
}, []);

const myPosts = useSelect(
    (select) =>
        select("core").getEntityRecords("postType", postType, {
            per_page: -1,
            orderby: "title",
            order: "asc",
            status: "publish",
            author: authorId,
        }),
    [postType, authorId]
);

You can then simply pass myPosts to the current PostControl component and it should work. Note that if using a custom post type, you need to add author to the supports array when registering it.

I'll see about adding possibility to pass additional query arguments to our useAllPosts hook, so you could easily pass along the author id etc.

mipon commented 2 years ago

Great, thank you.