1

Before Gutenberg I added meta_fields like "_my_integer_value" and added them to the post. I could query with something like this:

$args = array(
    'post_type'      => 'post',
    'orderby'        => array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ),
    'meta_key'       => '_my_integer_value',
    'posts_per_page' => '300',
    'order'          => 'DESC'
);

Now I made a block that saves its content to the html of the block.

block.json

"attributes": {
    "my_integer_value": {
        "type": "string",
        "source": "html",
        "selector": "span.value",
        "default": 5
    }
},

What is the best practice to use blocks and make information in them sortable in queries? Can I save the contents to the post meta somehow? Is there another option to save content in block that can be queried? This allows only for queries for posts that have this block but I can't sort them: WP Query by Gutenberg block and get its attribute

16
  • 1
    you don't, blocks are just content, so this is no different to asking how do I query for posts that contain a shortcode with attributes. You were never meant to query for blocks, nevermind sort them by it. If you want to store some information then query for posts with it, you do what was always the best practice, use a term in a taxonomy. The block editor does not change that. Blocks are a sub-unit of content, and a post is the smallest chunk of content as far ass WP_Query is concerned. You can search inside a post but it is no different to searching for words in a paragraph
    – Tom J Nowell
    Commented Nov 19, 2021 at 14:34
  • Thank Tom, as always. So how do I save the data in the taxonomy from within the blocks plugin? Is there a best-practise? Or is a meta_key with "add_post_meta( $post_ID, $meta_name, $meta_value, true ) " the same a taxonomy?
    – Marc
    Commented Nov 19, 2021 at 14:40
  • I don't know that your approach is the correct one, and it's difficult to advise without knowing the purpose of the block. And no add_post_meta adds post meta, whereas taxonomy/terms are a custom taxonomy like categories/tags. Post meta is fast when you already know the post ID. Taxonomy tables are purpose built for finding post IDs when you know the term ( the thing meta_query does but potentially hundreds of times faster and more scalable and using dedicated tables )
    – Tom J Nowell
    Commented Nov 19, 2021 at 14:53
  • Normally if you need to set a meta or term or anything that's attached to the post, it would be by registering a sidebar plugin or adding a panel to the post inspector. A block might fetch these values for display but while nothing prevents an edit component from setting them, it's not how the UX was intended to work.
    – Tom J Nowell
    Commented Nov 19, 2021 at 14:55
  • Alright. Thank you. I wanted to port my game review plugin "wp-shortscore" wordpress.org/plugins/wp-shortscore to a block: github.com/mtoensing/game-review Shortscore uses a panel to input the score and displays the score at the end of the post. I get a feeling that this is the best approach for this kind of problem
    – Marc
    Commented Nov 19, 2021 at 15:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.