Custom Fields
Re-Envisioned
Support MB Blocks Custom query with taxonomy in a gutenberg block.

  • Creator
    Topic
  • #31340

    Hello, In my case I want to make a custom query using taxonomies. I have a CPT of books and a CPT of topics. I want to have a gutenberg block template, where I can relate these custom post type. Example: In CPT Topics, have a post called: Drawing Books, and in CPT Books, have all the books related to that topic. In the post of Drawing Books, to insert blocks to separate the books by sub-topics, and as a result to have a block that consults the "Basic Drawing Books" and the block that follows consults "Animated Drawing Books".

    I have been trying to do it this way:

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        $meta_boxes[] = [
            'title' => 'Books',
            'id'    => 'book',
            'type'  => 'block', // Important.
            'icon'  => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path fill="currentColor" d="M485.5 0L576 160H474.9L405.7 0h79.8zm-128 0l69.2 160H149.3L218.5 0h139zm-267 0h79.8l-69.2 160H0L90.5 0zM0 192h100.7l123 251.7c1.5 3.1-2.7 5.9-5 3.3L0 192zm148.2 0h279.6l-137 318.2c-1 2.4-4.5 2.4-5.5 0L148.2 192zm204.1 251.7l123-251.7H576L357.3 446.9c-2.3 2.7-6.5-.1-5-3.2z"></path></svg>',
            'category' => 'layout',
            'supports' => [
                'align' => ['wide', 'full'],
            ],
            'render_template' => get_stylesheet_directory() . '/blocks/book/template.php', // The PHP template that renders the block.
            'enqueue_style'   => get_stylesheet_directory_uri() . '/blocks/book/style.css', // CSS file for the block.
    
            'fields' => [
                [
                    'type' => 'text',
                    'id'   => 'title',
                    'name' => 'Title',
                ],
                [
                    'type' => 'textarea',
                    'id'   => 'content',
                    'name' => 'Content',
                ],
                [
                    'type' => 'color',
                    'id'   => 'background_color',
                    'name' => 'Background Color',
                ],
    
                array(
                    'name'       => 'Taxonomy',
                    'id'         => 'taxonomy_books',
                    'type'       => 'taxonomy',
                    'taxonomy'   => 'book',
                    'field_type' => 'select_advanced',
                ),
            ],
        ];
        return $meta_boxes;
    } );

    This is my template file

    <div id="<?= $id ?>" class="<?= $class ?>" style="background-color: <?= mb_get_block_field( 'background_color' ) ?>">
    
        <div class="books__body">
            <h2><?php mb_the_block_field( 'title' ) ?></h2>
    
            <div class="books__line"></div>
    
            <div class="books__content"><?php mb_the_block_field( 'content' ) ?></div>
    
            <?php  
       $current_post_id = get_the_ID();
    
    $books = $term = rwmb_meta( 'taxonomy_books');
    $queryBooks = new WP_query(array(
        'posts_per_page' => -1,
        'post_type' => 'books',
        'tax_query' => array(
            array (
                'taxonomy' => 'book',
                'terms'    => $books,
            ) ),
    ));
    
    while($queryBooks->have_posts()) {
    $queryBooks->the_post(); ?>
    
    <div class="books-query">
        <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
        <p><?php the_excerpt(); ?> <a href="<?php the_permalink(); ?>" class="nu gray">Read Now</a></p>
        </div>
    </div>

    As you can see my intention is to create a field in the block to select the tag to query.

    In this way.

    null

    This is the tag that the books must have

    This is the block where I want to select the tag to make the query

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #31358
    Long Nguyen
    Moderator

    Hi,

    The field taxonomy doesn’t store any terms in the post meta. Instead, it sets post terms. Just like a replacement of Category or Tag meta box of WordPress so it does not work with Group or Block. If you want to save the taxonomy as the post meta or Block content, please use the field taxonomy_advanced.

    For more information, please follow these documentations.
    https://docs.metabox.io/fields/taxonomy-advanced/
    https://docs.metabox.io/fields/taxonomy/#data

    #31405

    Greetings, although I used the taxonomy_advanced field, I did not get what I expected.

    Is something still missing?

    #31408
    Long Nguyen
    Moderator

    Hi,

    To get the field value in the block, you can use the helper function mb_get_block_field(). In this case, it will return a term object and you should add the argument field to the tax_query array. The code should be

    $books = mb_get_block_field( 'taxonomy_books');
    $queryBooks = new WP_query(array(
        'posts_per_page' => -1,
        'post_type' => 'books',
        'tax_query' => array(
            array (
                'taxonomy' => 'book',
                'terms'    => $books->term_id,
                'field'    => 'term_id', //optinal, default is term_id, just for declaration
            ) ),
    ));

    Get more details on the documentation
    https://docs.metabox.io/extensions/mb-blocks/#render_callback
    https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

    #31475
    $books = mb_get_block_field( 'taxonomy_books');
    $queryBooks = new WP_query(array(
        'posts_per_page' => -1,
        'post_type' => 'books',
        'tax_query' => array(
            array (
                'taxonomy' => 'book',
                'terms'    => $books->taxonomy_books,
                'field'    => 'taxonomy_books',
            ) ),
    ));

    This is what my code would look like.

    But the query is not what I expected.

    As you can see in the image, the query brings me the values of all the posts that have a tag checked, but not the specific tag. But also, when marking the field in the block, it brings me the value of the taxonomy itself.

    #31480
    Long Nguyen
    Moderator

    Hi,

    The field taxonomy_books is not a possible value of the key field. Possible values are term_id, name, slug or term_taxonomy_id. Default value is term_id

    Follow my previous code to get posts from a specific term

    $books = mb_get_block_field( 'taxonomy_books');
    $queryBooks = new WP_query(array(
        'posts_per_page' => -1,
        'post_type' => 'books',
        'tax_query' => array(
            array (
                'taxonomy' => 'book',
                'terms'    => $books->term_id,
                'field'    => 'term_id', //optional, default is term_id, just for declaration
            ) ),
    ));

    Please read carefully on the documentation https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

    Regarding the value of taxonomy also display on the frontend, you need to check your template code if there is a line of code that displays the term title.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.