skip to Main Content

I specified category id as follows, but still query all posts, not posts in category 4 (term_id).

<!-- wp:query {"query":{"category":4,"perPage":0,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
    <!-- wp:post-template -->
        <!-- wp:post-title {"isLink":true} /-->
    <!-- /wp:post-template -->
<!-- /wp:query -->

2

Answers


  1. Chosen as BEST ANSWER

    thx, i fix it using:

    <!-- wp:query {"query": {"categoryIds": [4], "perPage": 0, "pages": 0, "offset": 0, "postType": "post", "order": "ASC", "orderBy": "date"}} -->
        <!-- wp:post-template -->
            <!-- wp:post-title {"isLink": true} /-->
        <!-- /wp:post-template -->
    <!-- /wp:query -->
    

    1. Firstly, go to visual mode in the WP block editor.

    2. Select Query loop block in the editor

    3. In inspector control (on the right side of text editor) select Block panel

    4. Under Block panel, go to Filters (click the + icon)

    5. Select Taxonomies option, then type the desired category name

    6. Make sure now the query loop only shows the filtered posts

    7. Now turn the editor mode to code editor, you will see the taxQuery like this

      <!-- wp:query { ... "taxQuery":{"category":[23]}}} -->

    Query Loop Filter Option


    Source from Gutenberg:

    Function which build WP_Query arguments from the block’s JSON data:

    tax_query array formation from block’s attribute query.taxQuery:

    Snippet:

    if ( ! empty( $block->context['query']['taxQuery'] ) ) {
        $query['tax_query'] = array();
        foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
            if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
                $query['tax_query'][] = array(
                    'taxonomy'         => $taxonomy,
                    'terms'            => array_filter( array_map( 'intval', $terms ) ),
                    'include_children' => false,
                );
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search