Support

Account

Home Forums Backend Issues (wp-admin) How to check a box when saving a post?

Solved

How to check a box when saving a post?

  • I have two checkboxes on a post and I’m looking for a way to check a selected box when saving a post.

    The two checkboxes are under ‘status_active’ and the checkbox is called ‘status_locked’ and I’m looking for a way to check ‘status_locked’ in ‘status_active’ when saving the post.

    I guess the best way would be to update the field and save it as ‘TRUE’?

    Code so far:

    function post_extra_save( $post_id, $post){
        global $pagenow;
        if ($pagenow == 'post.php') {
                if (! in_array( 'status_locked', get_field('status_active', $post_id) )) {
                };
    };
    add_action( 'save_post', 'post_extra_save', 10, 2 );
  • It it is a checkbox field then you need to update it with an array of the values you want checked, each value is not stored separately.

    
    $status_active = get_field('status_active', $post_id);
    if (!in_array('status_locked', $status_active)) {
      $status_active[] = 'status_locked';
      update_field('status_active', $status_active, $post_id);
    };
    
  • What if there’s multiple choices?

    $status_active = get_field('breaking_news_status', $post_id) == 'active';
                    if (!in_array('breaking_news_status', $status_active)) {
                        $status_active[] = 'status_locked';
                        update_field('breaking_news_status', $status_active, $post_id);
                    };
  • never mind, figured it out

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.