Custom Fields
Re-Envisioned
Support MB Views Cannot access values and labels in a checkbox field loop

  • Creator
    Topic
  • #28540
    Resolved Jon Whipple
    Participant

    I have created a view that is set to appear on a particular page on my site. The view collects and displays data from a Custom Post Type using fields that I made using MB Custom Fields. I am doing this because I need custom HTML, CSS, and JavaScript for user display.

    The View displays on the correct page, and loops though the Custom Post's Custom Fields and displays them EXCEPT for the fields in a checkbox array.

    I am using the Insert Field button and it inserts a loop. Do I need to modify this somehow? I am sure it's something about scope or accessing field values and labels that I just don't know how to do in a loop within a loop.

    Can anybody tell me how to do this?

    Code for View assigned to page

    {% set args = {post_type: 'client', posts_per_page: -1} %}
    
    {% set spotlights = mb.get_posts( args )|sort((a, b) => a.post_title <=> b.post_title) %}
    
    {% for post in spotlights %}
    
        {% if post.client_spotlight == 1 %}
    
            <div class="client spotlight">
                <p>
                    {{ post.post_title }}
                </p>
                <p>
                    {{ post.post_excerpt }}
                </p>
                <p>
                    {{ post.client_website }}
                </p>
                <p>
                    <b>Spotlight</b>
                </p>
                <p>
                    <b>Kind of business</b> 
                    {% for item in post.kind_of_business %}
                        {{ item.label }}
                    {% endfor %}
                </p>
                <hr>
            </div>
    
        {% endif %}
    
    {% endfor %}
    
    {% set clients = mb.get_posts( args )|sort((a, b) => a.post_title <=> b.post_title) %}
    
    {% for post in clients %}
    
        {% if post.client_spotlight != 1 %}
    
            <div class="client">
                <p>
                    {{ post.post_title }}
                </p>
                <p>
                    {{ post.post_excerpt }}
                </p>
                <p>
                    {{ post.client_website }}
                </p>
                <p>
                    <b>NOT</b> Spotlight
                </p>
                <p>
                    <b>Kind of business</b> 
                    {% for item in post.kind_of_business %}
                        {{ item.label }}
                    {% endfor %}
                </p>
                <hr>
            </div>
    
        {% endif %}
    
    {% endfor %}

    Type: Singular
    Location: Page, Specified Page
    Render for: Only the post content area
    Position: After the post content
    Order: 0
    Name: who-we-do-stuff-for

    Code for Custom Fields

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'      => __( 'Client', 'your-text-domain' ),
            'id'         => 'client',
            'post_types' => ['client'],
            'context'    => 'after_title',
            'fields'     => [
                [
                    'name' => __( 'Website', 'your-text-domain' ),
                    'id'   => $prefix . 'client_website',
                    'type' => 'url',
                ],
                [
                    'type' => 'divider',
                ],
                [
                    'name'          => __( 'Spotlight', 'your-text-domain' ),
                    'id'            => $prefix . 'client_spotlight',
                    'type'          => 'switch',
                    'style'         => 'rounded',
                    'admin_columns' => [
                        'position' => 'after kind_of_business',
                        'sort'     => true,
                    ],
                ],
                [
                    'type' => 'divider',
                ],
                [
                    'name'              => __( 'Kind of business', 'your-text-domain' ),
                    'id'                => $prefix . 'kind_of_business',
                    'type'              => 'checkbox_list',
                    'label_description' => __( 'Used to collect and sort client lists. Check all that apply.', 'your-text-domain' ),
                    'options'           => [
                        'business'       => __( 'Business', 'your-text-domain' ),
                        'public_sector'  => __( 'Public sector', 'your-text-domain' ),
                        'community'      => __( 'Community (non-profits)', 'your-text-domain' ),
                        'sustainability' => __( 'Sustainability (environmental non-profits and businesses)', 'your-text-domain' ),
                        'indigenous'     => __( 'Indigenous (Indigenous organizations)', 'your-text-domain' ),
                    ],
                    'std'               => ['business'],
                    'required'          => true,
                    'admin_columns'     => [
                        'position' => 'after title',
                        'sort'     => true,
                    ],
                ],
                [
                    'type' => 'divider',
                ],
                [
                    'type' => 'heading',
                    'name' => __( 'Projects', 'your-text-domain' ),
                ],
                [
                    'id'          => $prefix . 'client_project',
                    'type'        => 'group',
                    'collapsible' => true,
                    'save_state'  => true,
                    'group_title' => '{#} – {client_project_name}',
                    'clone'       => true,
                    'add_button'  => __( 'Add another project', 'your-text-domain' ),
                    'fields'      => [
                        [
                            'name' => __( 'Project Name', 'your-text-domain' ),
                            'id'   => $prefix . 'client_project_name',
                            'type' => 'text',
                            'desc' => __( 'If you have one or refer to this work in a regular way, this might be handy', 'your-text-domain' ),
                        ],
                        [
                            'name' => __( 'Project Description', 'your-text-domain' ),
                            'id'   => $prefix . 'client_project_description',
                            'type' => 'wysiwyg',
                            'desc' => __( 'Describe the work and results for the client', 'your-text-domain' ),
                        ],
                        [
                            'name' => __( 'Project Date', 'your-text-domain' ),
                            'id'   => $prefix . 'project_date',
                            'type' => 'date',
                            'desc' => __( 'Choose the engagement date (approximate is fine)', 'your-text-domain' ),
                        ],
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }

    Code for the Custom Post

    <?php
    add_action( 'init', 'your_prefix_register_post_type' );
    function your_prefix_register_post_type() {
        $labels = [
            'name'                     => esc_html__( 'Clients', 'your-textdomain' ),
            'singular_name'            => esc_html__( 'Client', 'your-textdomain' ),
            'add_new'                  => esc_html__( 'Add New', 'your-textdomain' ),
            'add_new_item'             => esc_html__( 'Add new client', 'your-textdomain' ),
            'edit_item'                => esc_html__( 'Edit Client', 'your-textdomain' ),
            'new_item'                 => esc_html__( 'New Client', 'your-textdomain' ),
            'view_item'                => esc_html__( 'View Client', 'your-textdomain' ),
            'view_items'               => esc_html__( 'View Clients', 'your-textdomain' ),
            'search_items'             => esc_html__( 'Search Clients', 'your-textdomain' ),
            'not_found'                => esc_html__( 'No clients found', 'your-textdomain' ),
            'not_found_in_trash'       => esc_html__( 'No clients found in Trash', 'your-textdomain' ),
            'parent_item_colon'        => esc_html__( 'Parent Client:', 'your-textdomain' ),
            'all_items'                => esc_html__( 'All Clients', 'your-textdomain' ),
            'archives'                 => esc_html__( 'Client Archives', 'your-textdomain' ),
            'attributes'               => esc_html__( 'Client Attributes', 'your-textdomain' ),
            'insert_into_item'         => esc_html__( 'Insert into client', 'your-textdomain' ),
            'uploaded_to_this_item'    => esc_html__( 'Uploaded to this client', 'your-textdomain' ),
            'featured_image'           => esc_html__( 'Featured image', 'your-textdomain' ),
            'set_featured_image'       => esc_html__( 'Set featured image', 'your-textdomain' ),
            'remove_featured_image'    => esc_html__( 'Remove featured image', 'your-textdomain' ),
            'use_featured_image'       => esc_html__( 'Use as featured image', 'your-textdomain' ),
            'menu_name'                => esc_html__( 'Clients', 'your-textdomain' ),
            'filter_items_list'        => esc_html__( 'Filter clients list', 'your-textdomain' ),
            'filter_by_date'           => esc_html__( '', 'your-textdomain' ),
            'items_list_navigation'    => esc_html__( 'Clients list navigation', 'your-textdomain' ),
            'items_list'               => esc_html__( 'Clients list', 'your-textdomain' ),
            'item_published'           => esc_html__( 'Client published', 'your-textdomain' ),
            'item_published_privately' => esc_html__( 'Client published privately', 'your-textdomain' ),
            'item_reverted_to_draft'   => esc_html__( 'Client reverted to draft', 'your-textdomain' ),
            'item_scheduled'           => esc_html__( 'Client scheduled', 'your-textdomain' ),
            'item_updated'             => esc_html__( 'Client updated', 'your-textdomain' ),
            'text_domain'              => esc_html__( 'your-textdomain', 'your-textdomain' ),
        ];
        $args = [
            'label'               => esc_html__( 'Clients', 'your-textdomain' ),
            'labels'              => $labels,
            'description'         => 'A Client post is a full description of a client organization and the work that Boldt has done for them.',
            'public'              => true,
            'hierarchical'        => false,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'show_ui'             => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'show_in_rest'        => true,
            'query_var'           => true,
            'can_export'          => true,
            'delete_with_user'    => true,
            'has_archive'         => true,
            'rest_base'           => '',
            'show_in_menu'        => true,
            'menu_icon'           => 'dashicons-businessman',
            'menu_position'       => 20,
            'capability_type'     => 'post',
            'supports'            => ['title', 'excerpt', 'revisions', 'page-attributes', 'post-formats'],
            'taxonomies'          => ['post_tag'],
            'rewrite'             => [
                'with_front' => false,
            ],
        ];
    
        register_post_type( 'client', $args );
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
    Replies
  • #28541
    Jon Whipple
    Participant

    I need to display the label or values from the post's Kind of Business field which is a checkbox list.

    #28567
    Long Nguyen
    Moderator

    Hi Jon,

    Please follow this topic to know how to get custom field value in a custom query https://metabox.io/support/topic/mb-view-by-shortcode/

    Retrieve checkbox list value will get an array so you need to use a loop to show all value.
    https://docs.metabox.io/fields/checkbox-list/

    If you want to get the label, please follow on this topic https://metabox.io/support/topic/label-not-rendered/

    #28568
    Jon Whipple
    Participant

    Thanks, Long. I appreciate your notes and links. Not afraid of PHP and Twig and the like, just a novice and am trying to learn. I will be able to study and try these later today.

    As I understand things at first glance (before really getting into it)

    • I am doing something here that is called a "custom query" that's a term I have seen in a lot of Different WordPress conversations, but I still don't know what it actually means. I wonder what the difference is between a query and a custom query and why it matters? (No need to answer anybody unless you want to)
    • I am using a loop to try and get the value in the checkbox list, it's just the wrong loop. I need to access the data I want using rwmb_meta(), and maybe other Meta Box helper functions
    • I can also use rwmb_the_value() and rwmb_get_field_settings() from my view to get some of the values. I don't yet know how to use these but I will learn.
    • Not sure how to know/determine when I can simply use the MB Views' Insert Field functionality, or when I have to use a rwmb_ helper function. Trial and error I guess (I have read the definitions of these functions but they are just beyond the edge of my limited programming knowledge.

    I'll add notes here about my progress.

    #28585
    Jon Whipple
    Participant

    Hi Long,

    I am trying to follow along with all of this, but I think I am missing some key things because I am a novice.

    I can get an array from the checkbox field using
    {% set kob = mb.rwmb_meta( 'kind_of_business', '', post.ID ) %}
    This works and if I
    {{ kob }} I see the word Array in the layout.
    This is good.

    The checkbox list docs https://docs.metabox.io/fields/checkbox-list/ give this example in PHP

    $values = rwmb_meta( $field_id );
    foreach ( $values as $value ) {
        echo $value;
    }

    and then add
    rwmb_the_value( $field_id ); to display the field label. I wouldn't know how to use this function in relation to the previous one.

    My best guess at trying to do this in Twig is

    {% set kob = mb.rwmb_meta( 'kind_of_business', '', post.ID ) %}
    
    {# now I have an Array named kob printing kob results in Array being displayed. YAY! #}
    
    {% for item in kob %}
        {% set type = mb.rwmb_the_value( 'kind_of_business' ) %}
        {{ type }}
    {% endfor %}

    Which doesn't work. the rwmb_the_value() function requires the field name but I don't think that field exists within the array?

    #28595
    Long Nguyen
    Moderator

    Hi Jon,

    kob is an array of checkbox list values, you can echo item in the loop to show each value

    {% for item in kob %}
        {{ item }} <br>
    {% endfor %}

    to show the checkbox list labels, just use the function rwmb_the_value() outside the loop

    {{ mb.rwmb_the_value( 'kind_of_business', '', post.ID, false ) }}

    Regarding the custom query, I've updated the documentation here
    https://docs.metabox.io/extensions/mb-views/#custom-query
    https://docs.metabox.io/extensions/mb-views/#main-query

    #28605
    Jon Whipple
    Participant

    Hi Long,

    OK, thanks, this is working now and outputting expected values.

    Fantastic and thank you.

    So as I understand it, a value in an array is referred to as an 'item'. I suppose if you know the key name you can get that specific item out of the array? I would have to find the syntax to do that.

    I put the {{ mb.rwmb_the_value( 'kind_of_business', '', post.ID, false ) }} function after the loop that was stepping through the array. It returns the value as an HTML list as I understood from the docs on that function.

    For those of you following along this is the code that is working for me. It makes a "client" tile for display on a client listing page. It selects all the post objects that are from my custom post type called Client it sorts them alphabetically and then for each of the clients that has the spotlight field set to true it writes HTML for the entry. You can see output of the loop through the business type using mb.rwmb_meta() function, followed by me using the {{ mb.rwmb_the_value( 'kind_of_business', '', post.ID, false ) }} after.

    {% set args = {post_type: 'client', posts_per_page: -1} %}
    {% set spotlights = mb.get_posts( args )|sort((a, b) => a.post_title <=> b.post_title) %}
    {% for post in spotlights %}
        {% if post.client_spotlight == 1 %}
            <div class="client spotlight">
                <h3>
                    <a href="{{ post.post_url }}">{{ post.post_title }}</a>
                </h3>
                <p>
                    {{ post.post_excerpt }}
                </p>
                <p>
                    {{ post.client_website }}
                </p>
                <p>
                    <b>Spotlight</b>
                </p>
                <p>
                    <b>Kind of business</b>
                    {% set kob = mb.rwmb_meta( 'kind_of_business', '', post.ID ) %}
                    {# now I have an Array named kob#}
                    {% for item in kob %}
                        {{ item }}<br>
                    {% endfor %}
                    {{ mb.rwmb_the_value( 'kind_of_business', '', post.ID, false ) }}
                </p>
                <hr>
            </div>
        {% endif %}
    {% endfor %}

    Here is a screen grab of the incomplete card
    https://share.getcloudapp.com/4gunbG56

    #28606
    Jon Whipple
    Participant

    Oh also, Long,

    Thank you for updating those pages describing using the functions, the examples in Twig as well as PHP help guys like me learn how logic is expressed using different languages and markup schemes. I really appreciate it a lot.

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