How do I Manually (via PHP) set values for a Checkbox List...?

Support General How do I Manually (via PHP) set values for a Checkbox List...?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #37074
    NhymnbleNhymnble
    Participant

    I've looked through the documentation, and missed it if it is in there, but can't find an example of manually (via PHP) setting the values for a Checkbox List. Could anyone give me an example?

    #37079
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the WordPress function add_post_meta() with the last parameter set to false. For example:

    add_post_meta( 123, 'checkbox_list_vk9atamt05c', 'value1', false );

    where 123 is the post ID. Read more on the documentation
    https://developer.wordpress.org/reference/functions/add_post_meta/
    https://docs.metabox.io/fields/checkbox-list/#data

    #37092
    NhymnbleNhymnble
    Participant

    Thanks for this code, it works great on it's own. Here is my use case:

    I have a select field with the following values ('studio', '1 bed', '2 beds', '3 beds',...'10 beds'. I am using Gruid Builder WP to create facets. I need my facets for Bedrooms to be studio, 1 bed, 2 beds, 3 beds, 4 beds, and 5+ beds. My thought is using a checkbox list with those values which I will populate before save based on the number of beds chosen in my select field. Then I can just create a facet based off the checkbox list. How do I use the add_post_meta function with rwmb_before_save_post? add_post_meta requires a post ID, but I'm not sure how to retrieve that when utilizing the rwmb_before_save_post... Sorry for the noobish questions.

    #37099
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The action hook rwmb_before_save_post support passing the object ID (post ID) to the function inside. You can use it in your code

    add_action( 'rwmb_event-details_before_save_post', function( $post_id ) {
        add_post_meta( $post_id, 'checkbox_list_id', 'value1', false );
    } );

    Read more on the documentation
    https://docs.metabox.io/actions/rwmb-before-save-post/

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