Support

Account

Home Forums General Issues WP archive with two or more filter Reply To: WP archive with two or more filter

  • I have the same issue, however, I was able to figure out how to get the url to work by modifying the meta_query in functions.php:

    
    $meta_query[] = array( 
    	    		relation => 'AND',
    	    		array(
    	                'key'		=> 'community',
    	                'value'		=> $community,
    	                'compare'	=> 'IN',
                    ),
                    array(
    	                'key'		=> 'listing_status',
    	                'value'		=> $status,
    	                'compare'	=> 'IN',
                    ),
                );
    

    So, when I visit http://www.website.com/listings/?listing_status=sold&community=cityname, if I have a Listing that is both sold and in cityname, the listing is displayed and my checkboxes show in the sidebar. However, as soon as my URL changes to something like http://www.website.com/listings/?listing_status=sold&community=newcityname (where “newcityname” is not marked as “sold”) I get the following error when where the checkboxes normally show:

    Warning: Invalid argument supplied for foreach() in /serverpath/wp-content/themes/themename/archive-listings.php on line 27
    (where line 27 is my foreach loop)

    Here’s my code:

    
    <?php 
    				$field = get_field_object('community');
    				$values = isset($_GET['community']) ? explode(',', $_GET['community']) : array();
    				
    				$field2 = get_field_object('listing_status');
    				$values2 = isset($_GET['listing_status']) ? explode(',', $_GET['listing_status']) : array();
    				?>
    				<ul class="communities">
    					<?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>
    						<li>
    							<input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
    						</li>
    					<?php endforeach; ?>
    				</ul>
    				<ul class="status">
    					<?php foreach( $field2['choices'] as $choice_value2 => $choice_label2 ): ?>
    						<li>
    							<input type="checkbox" value="<?php echo $choice_value2; ?>" <?php if( in_array($choice_value2, $values2) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label2; ?></li>
    						</li>
    					<?php endforeach; ?>
    				</ul>
    

    How can I fix this?

    Also, how can I do the URL replacement when someone clicks on the checkboxes? I have the below setup which works for one custom field parameter at a time but not both:

    
    <script type="text/javascript">
        //Listing URL Query Change
    	(function($) {
    		$('.communities').on('change', 'input[type="checkbox"]', function(){
    			// vars
    			var $ul = $(this).closest('ul'),
    				vals = [];
    
    			$ul.find('input:checked').each(function(){
    
    				vals.push( $(this).val() );
    
    			});
    
    			vals = vals.join(",");
    
    			window.location.replace('<?php echo home_url('listings'); ?>?community=' + vals);
    		});
    		$('.status').on('change', 'input[type="checkbox"]', function(){
    			// vars
    			var $ul = $(this).closest('ul'),
    				vals = [];
    
    			$ul.find('input:checked').each(function(){
    
    				vals.push( $(this).val() );
    
    			});
    
    			vals = vals.join(",");
    
    			window.location.replace('<?php echo home_url('listings'); ?>?listing-status=' + vals);
    		});
    	})(jQuery);	
    </script>
    

    Thanks in advance for any help 🙂