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?

Share Improve this question asked Feb 8, 2024 at 7:53 JDQJDQ 1595 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You 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