0
votes

I am trying to set filters based on Advanced Custom Fields in my WordPress site. Basically my advanced custom field named as 'ispremium' has two values as 'yes' and 'no' and in dropdown filter I have set two options as 'Premium Only' And 'All Programs'.

I need to do when 'Premium Only' dropdown option is selected it will list all the post having value 'ispremium'='yes' and when all program is selected it will list both 'ispremium=yes' and 'ispremium='no'. I have following code but it is listing post with 'ispremium=yes' always. What's wrong in my code?

<select name="order" onchange="this.form.submit();">
    <?php
        $order_options = array(
            'yes' => 'Premium Only',
            'no' => 'All Programs',
        );

        $result = query_posts( array(
        'post_type' => 'post',
        'meta_query' => array(
        array(
        'key' => 'ispremium',
        'value' => 'yes',          
        ),
        ) ) );

        $result = query_posts( array(
        'post_type' => 'post',
        'meta_query' => array(
        array(
        'key' => 'ispremium',
        'value' => 'yes','no'          
        ),
        ) ) );


        foreach( $order_options as $result => $label ) {
            echo "<option ".selected( $_GET['value'], $value )." value='$value'>$label</option>";
        }
    ?>
</select>
1

1 Answers

1
votes

First, try to use WP_Query instead of query_posts(). You also have some syntax errors like 'value' => 'yes','no'.

For premium only:

$args = array(
    'post_type'     => 'post',
    'meta_query'    => array(
        array(
            'key'       => 'ispremium',
            'value'     => 'yes',
            'compare'   => '='
        )
    )
);

$the_query = new WP_Query( $args );

All Programs

$args = array(
    'post_type'     => 'post',
    'meta_query'    => array(
        'relation'      => 'OR',
        array(
            'key'       => 'ispremium',
            'value'     => 'yes',
            'compare'   => '='
        ),
        array(
            'key'       => 'ispremium',
            'value'     => 'no',
            'compare'   => '='
        )
    )
);

$the_query = new WP_Query( $args );

Check ACF documentation for further info. Also, I suggest you to do this with Ajax, since if you put this code within your select element, the available options are going to be the from the last query made, and won't be dynamic on field change.

But I don't totally get what you're trying to do though; you'll probably want to show the available programs in another drop down, or you could simply redirect to another archive page which lists the programs accordingly to the selected option.