I am new to wordpress .. so please be kind..:-)..
I was excited that WP could be used without knowing a lot of PHP. So I've gone ahead and created a block design and created a theme.. and it seems to be all working out well.
However now I am at an obstacle. I am able to create a query loop as below (I used it from the Twentytwentytwo theme) and it works fine for Posts (the regular post type) and I have a custom post type called 'Products' so I assumed that by changing the post type in the query to 'product' it should work.. but it does not.
<!-- wp:query {"query":{"perPage":10,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"layout":{"inherit":true},"tagName":""} -->
<!-- wp:post-template -->
<!-- wp:post-featured-image /-->
<!-- wp:post-title /-->
<!-- /wp:post-template -->
<!-- /wp:query -->
So my question is
- Is there a way to query custom post types in WordPress 5.9?
- If not, then can you guide me on how I can use the block design template and use PHP to achieve displaying custom post types in an archive view?
- Also can you please share some links on where I can find some info on how to create these queries by code.. basically the list of options available to use in the JSON format query option
Any help or advise would be appreciated.
I am new to wordpress .. so please be kind..:-)..
I was excited that WP could be used without knowing a lot of PHP. So I've gone ahead and created a block design and created a theme.. and it seems to be all working out well.
However now I am at an obstacle. I am able to create a query loop as below (I used it from the Twentytwentytwo theme) and it works fine for Posts (the regular post type) and I have a custom post type called 'Products' so I assumed that by changing the post type in the query to 'product' it should work.. but it does not.
<!-- wp:query {"query":{"perPage":10,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"layout":{"inherit":true},"tagName":""} -->
<!-- wp:post-template -->
<!-- wp:post-featured-image /-->
<!-- wp:post-title /-->
<!-- /wp:post-template -->
<!-- /wp:query -->
So my question is
- Is there a way to query custom post types in WordPress 5.9?
- If not, then can you guide me on how I can use the block design template and use PHP to achieve displaying custom post types in an archive view?
- Also can you please share some links on where I can find some info on how to create these queries by code.. basically the list of options available to use in the JSON format query option
Any help or advise would be appreciated.
Share Improve this question asked Feb 18, 2022 at 8:22 Alim BolarAlim Bolar 1314 bronze badges1 Answer
Reset to default 2I found a solution for this. So for those of you who face a similar situation, here's what I did (based on this solution : https://stackoverflow.com/questions/68871598/wordpress-query-loop-block-not-showing-custom-post-types)
So basically the custom post type that I had created needed to have this argument while registering the post-type in functions.php
'show_in_rest' => true
And then I just had to replace the post type in the query and all the results of the query showed up.
And for those using the Full Site Editor view in WordPress the Block section for query loop will then display the registered custom post type in addition to 'Post' and 'Page' in the drop-down menu.
So custom posts types are supported in the query loop and it works well!!..:-)..