Add posts select checkboxes using Advanced custom fields

I have two custom post types called hotels and rooms. Users can register to my web site and they can add their hotels and rooms from their wp backend. so my problem is when they are adding hotels they need to select their hotel rooms.

I’m using Advanced custom fields and it has field type called post object. but that field displaying posts in select menu. I need to do it with checkboxes.So it’ll be so easy to user.

Read More

I really appritiate if someone can help me.

Thanks

Related posts

Leave a Reply

1 comment

  1. It will require a little bt of code along with ACF

    Add checkbox field (ex : brands) and add following code in your functions.php file

    function my_acf_load_field( $field )
    {
        global $post;
        $field['choices'] = array();
        wp_reset_query();
        $query = new WP_Query(array('show_posts' => 100));
        foreach($query->posts as $product_id=>$macthed_product){
                $choices[$macthed_product->ID] = $macthed_product->post_title;
        }
        $field['choices'] = array();
    
        if( is_array($choices) )
        {
            foreach( $choices as $key=>$choice )
            {
                $field['choices'][$key] = $choice;
            }
        }
         wp_reset_query();
        return $field;
    }
    add_filter('acf/load_field/name=brands', 'my_acf_load_field');
    

    use wp query parameters to filter posts.

    Post id will be the checkbox value with title as label in metabox.