Get all MB field of a custom post type

Support MB REST API Get all MB field of a custom post type

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #40399
    komozuro@gmail.com[email protected]
    Participant

    Hi, I want to create a new API Route for Coupon, in which were added many custom fields from Metabox
    Could I query all the MB field values without listing all the field keys per post metadata
    Here is the working code that I create for the new route, I get the post meta value stored in Custom Table by the post_id.

    function get_custom_post_by_id_callback( $data ) {
    $post_id = $data['id'];
    $post = get_post( $post_id );
    $args = [
    'storage_type' => 'custom_table',
    'table' => 'wp_personal_coupons',
    ];
    $post_meta = rwmb_meta( 'voucher_entry', $args, $post_id );

    $response = array(
    'id' => $post->ID,
    'title' => $post->post_title,
    'content' => $post->post_content,
    'post_meta' => $post_meta
    );

    return $response;
    }

    add_action( 'rest_api_init', function () {
    register_rest_route( 'metabox', '/coupons/(?P<id>\d+)', array(
    'methods' => 'GET',
    'callback' => 'get_custom_post_by_id_callback',
    ));
    });

    Thanks.

    #40431
    PeterPeter
    Moderator

    Hello,

    If you are using the custom table, each custom field is a column and all field values of a post are saved to one row. So you can try to use an SQL query to get all field values associated with a post based on the provided post ID.

    For example: "SELECT * FROM your_table WHERE ID = { $post_id }"

    Please refer to the documentation https://docs.metabox.io/extensions/mb-custom-table/#query-posts-with-wp_query

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