Support

Account

Home Forums General Issues check box query

Helping

check box query

  • hello friends
    I made a checkbox field and it has 20 choice (
    Facilities hotel)
    I want to display facilities to my website but some hotele have 2 facilities and another one have 10. so I use query checkbox but it doesn’t work !

    look this is my html :

    <div class=”facilities”>
    <div class=”livicon” data-name=”medal” data-size=”25″ data-color=”#fff” ></div>
    title
    </div>

    I made check box choice like :
    <div class=”livicon” data-name=”medal” data-size=”25″ data-color=”#fff” ></div>
    title : TITLE

    and use this query:(‘facilities’ name of custom field)

    <?php

    $posts = get_posts(array(
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘facilities’, // name of custom field
    ‘value’ => ‘”red”‘, // matches exactly “red”
    ‘compare’ => ‘LIKE’
    )
    )
    ));
    <div class=”facilities”>
    if( $posts ) {
    <?php the_field(‘facilities’); ?>

    }
    </div>

    ?>

    what’s the problem ? thanks

  • Hi @saberap

    I’m afraid I don’t understand your goal. Could you please let me know the result you want?

    Also, it seems there are some syntax errors in your code. First, you forgot to add the PHP closing and opening tag before and after the div element. Second, kindly avoid using $posts variable as it’s sometimes used by WordPress. Third, you need to loop through the returned posts list to show the checkbox value for each post. And lastly, you need to pass the post ID because the code is executed outside of The Loop.

    So, it should be like this:

    <?php
    
    // use another variable than "$posts"
    $the_posts = get_posts(array(
        'meta_query' => array(
            array(
                'key' => 'facilities', // name of custom field
                'value' => '"red"', // matches exactly "red"
                'compare' => 'LIKE'
            )
        )
    ));
    
    // don't forget the PHP closing tag
    ?>
    
    <div class="facilities">
    
    <?php
    // and also the PHP opening tag
    
    if( $the_posts ) {
        
        // loop through the posts
        for( $the_posts as $the_post ){
            // show the value
            the_field('facilities', $the_post->ID );
        }
    
    }
    
    ?>
    </div>

    I hope this makes sense 🙂

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

The topic ‘check box query’ is closed to new replies.