Support

Account

Home Forums ACF PRO Sortby multiple filters from select dropdown

Helping

Sortby multiple filters from select dropdown

  • I’ve created a simple page that shows lists of properties (houses) and at this moment the user can choose to change the order of display by project price, project width etc from the choices in a select dropdown list. There is also an option to then sort by ascending or descending. This is all going perfectly BUT now the client wants to make it the drop down is a single option that might show things like ‘Project Price [low to high]’ and ‘Project Price [high to low]’.

    Problem is passing these values via the &_GET from the URL isn’t working i.e. you try to set the select options value to ‘feature=project_price&option=ASC’ but the URL just converts the ‘&’,’=’ to the special character code and of course that means the page sorting doesn’t work ( project_price%26options%3DASC)

    What is the best approach to getting a multiple filter to apply via a single dropdown choice? Below is the code I’m using for the filter options in the page:

    <form class="form-inline">
    
        <select name="feature" class="form-control">
            
            <?php
    
                if ($render_cat == "orangeries-traditional") : 
                    $orderby_options = array(
                        'project_number' 	=> 'Project Number',
                        'project_price&options=ASC'		=> 'Price [low to high]',
                        'project_width'		=> 'Project Width',
                        'bedrooms'			=> 'Bedrooms',
                    );
                elseif ($render_cat == "complexes") : 
                    $orderby_options = array(
                        'project_number' 	=> 'Project Number',
                        'project_price'		=> 'Price [low to high]',
                        'project_width'		=> 'Project Width',
                    );
                else :
                    $orderby_options = array(
                        'project_number' 	=> 'Project Number',
                        'project_price'		=> 'Project Price',
                        'project_width'		=> 'Project Width',
                    );
                endif;
                
    
                foreach( $orderby_options as $value => $label ) {
                    echo "<option ".selected( $_GET['feature'], $value )." value='$value'>$label</option>";
                }
                
            ?>
        </select>
        
        <select name="order" class="form-control">
    
            <?php
            
                $orderby_options = array(
                    'ASC' => 'Ascending',
                    'DESC' => 'Descending',
                );
    
                foreach( $orderby_options as $value => $label ) {
                    echo "<option ".selected( $_GET['order'], $value )." value='$value'>$label</option>";
                }
    
            ?>
        </select>
    
        <input type="submit" value="Go" class="btn btn-primary" />
    
    </form>

    and this is the query I’m using:

    // Let's make sure only the url path without any query parameters from searches are used.
    $path = parse_url($url, PHP_URL_PATH);
    
    // We only need the final part of the url as that should give us the active category.
    $render_cat = basename($path);
    
    $posts = array(
    	'posts_per_page'	=> -1,
    	'post_status'       => 'publish',
    	'post_type' 		=> 'renders',
    	'meta_query' 		=> array(
    		'relation' 	=> 'AND',
    		'prod_type' =>	array(
    			'key'  		=> 'product_type',
    			'value' 	=> $render_cat,
    			'compare' 	=> 'LIKE',
    		),
    	),
    	'meta_key'  		=> $_GET['feature'],
    	'orderby'   		=> 'meta_value',
    	'order'     		=> $_GET['order'],
    
    );
  • You cannot set a value to something like project_price&options=ASC

    You will need to parse the value that is submitted.

    I would suggest something like

    
    orderby:project_price;order=ASC
    
    
    $posts = array(
    	'posts_per_page'	=> -1,
    	'post_status'       => 'publish',
    	'post_type' 		=> 'renders',
    	'meta_query' 		=> array(
    		'relation' 	=> 'AND',
    		'prod_type' =>	array(
    			'key'  		=> 'product_type',
    			'value' 	=> $render_cat,
    			'compare' 	=> 'LIKE',
    		),
    	),
    	'orderby'   		=> 'meta_value',
    
    );
    $settings = explode(';', $_GET['feature']);
    foreach ($settings as $setting) {
      $parts = explode(':', $setting);
      $posts[$parts[0]] = $parts[1];
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.