admin管理员组文章数量:1122846
I know how to query all posts in a certain category by slug with WP_Query
. What I would like to do is the same with the core/query
block markup. I am able to get it working with the category ID, but I'd like to do it by slug as the target category has a different ID in development and production installations. I know I could check the domain, but I'd rather save a check if possible.
'news' is the target category slug. I've tried the following:
<!-- wp:query {"query":{"postType":"post","author":"","search":"","exclude":[],"taxQuery":{"taxonomy":"category","field":"slug","terms":["news"]}}} -->
<!-- wp:query {"query":{"postType":"post","author":"","search":"","exclude":[],"categoryName":"news"}} -->
but both show all the posts. As far as I understand it, the markup passes the query arguments unmodified into the underlying WP_Query
function, but maybe this is incorrect?
I know how to query all posts in a certain category by slug with WP_Query
. What I would like to do is the same with the core/query
block markup. I am able to get it working with the category ID, but I'd like to do it by slug as the target category has a different ID in development and production installations. I know I could check the domain, but I'd rather save a check if possible.
'news' is the target category slug. I've tried the following:
<!-- wp:query {"query":{"postType":"post","author":"","search":"","exclude":[],"taxQuery":{"taxonomy":"category","field":"slug","terms":["news"]}}} -->
<!-- wp:query {"query":{"postType":"post","author":"","search":"","exclude":[],"categoryName":"news"}} -->
but both show all the posts. As far as I understand it, the markup passes the query arguments unmodified into the underlying WP_Query
function, but maybe this is incorrect?
2 Answers
Reset to default 1You should not modify the taxQuery
value, or that it should always be in the form of {"<taxonomy>":[<term IDs>],"<taxonomy>":[<term IDs>],"<taxonomy>":[<term IDs>],...}
.
But you can add custom arguments, so "categoryName":"news"
is good.
And then you can use the query_loop_block_query_vars
filter to add the category slug to the WP_Query
arguments coming from the Query Loop block.
Here's an example where I used tax_query
( and not category_name
):
// `categoryName` is not part of `$query` (because `categoryName` is a custom
// argument), but it is stored in the `$block->context['query']` array, which
// explains why we need the second parameter, i.e. `$block`.
function my_filter_query_loop_block_query_vars( $query, $block ) {
if ( ! empty( $block->context['query']['categoryName'] ) ) {
$tax_query = isset( $query['tax_query'] ) ? (array) $query['tax_query'] : array();
$tax_query[] = array(
'taxonomy' => 'category',
'terms' => $block->context['query']['categoryName'],
'field' => 'slug',
);
$tax_query['relation'] = 'OR';
$query['tax_query'] = $tax_query;
}
return $query;
}
add_filter( 'query_loop_block_query_vars', 'my_filter_query_loop_block_query_vars', 10, 2 );
I sure you can use just
..."taxQuery":{"category":[1]}...
It works for me.
本文标签: theme developmentQuerying by post category slug with corequery block markup
版权声明:本文标题:theme development - Querying by post category slug with corequery block markup 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303765a1931997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论