Support

Account

Home Forums General Issues Querying with meta query on true/false field

Helping

Querying with meta query on true/false field

  • I have a true false field (hide_on_public_page)

    here is my code in my template:

    // The Query
    $today = date (‘Ymd’, strtotime(‘-6 hours’));
    $future = date (‘Ymd’, strtotime(‘+4 Months’));

    $event_query = new WP_Query(
    array(
    ‘post_type’ => ‘al_event’,
    ‘orderby’ => ‘menu_order’,
    ‘order’ => ‘ASC’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘event_date_end’,
    ‘compare’ => ‘>=’,
    ‘value’ => $today,
    ),
    array(
    ‘key’ => ‘event_date’,
    ‘compare’ => ‘<=’,
    ‘value’ => $future,
    ),
    array(
    ‘key’ => ‘hide_on_public_page’,
    ‘value’ => ‘0’,
    )
    ),
    ) );

    This code works, but the problem is that I want to show the fields unless “hid_on_public_page” has been clicked. This seems to only work if checked otherwise there is no value in the field. What would I do to make sure that the posts show up if a user hasn’t clicked that true/false field?

  • This is how I got it to work, but it seems there should be an easier way?

    $event_query = new WP_Query(     
        array(
            'post_type' => 'al_event',
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'meta_query' => array(
                array(
                    'key'     => 'event_date_end',
                    'compare' => '>=',
                    'value'   => $today,
                ),
                array(
                    'key'     => 'event_date',
                    'compare' => '<=',
                    'value'   => $future,
                ),
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'hide_on_public_page',
                        'value' => '0'
                    ),
                    array(
                        'key' => 'hide_on_public_page',
                        'compare' => 'NOT EXISTS', 
                    )
                )
            ),
        ) );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.