Home › Forums › General Issues › WP archive with two or more filter
Hello,
I use the following tutorial to create a checkbox filter for my blog.
http://www.advancedcustomfields.com/resources/tutorials/creating-wp-archive-custom-field-filter/
This tutorial show only how to create a filter for one custom filter. I would to create two or more filter.
How can I do this?
Thanks
All you would need to do us add more meta query value sets. For example adding the following code after the first meta query section should do the trick.
$bathrooms = explode(',', $_GET['bathrooms']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'bathrooms',
'value' => $bathrooms,
'compare' => 'IN',
);
Then you can add a bathrooms get param to the URL. Eg http://mysite.com?bedrooms=1&bathrooms=2
Hi,
doesn’t work.
in function.php:
add_action(‘pre_get_posts’, ‘my_pre_get_posts’);
function my_pre_get_posts( $query )
{
// validate
if( is_admin() )
{
return;
}
if( !$query->is_main_query() )
{
return;
}
$meta_query = $query->get(‘meta_query’);
if( !empty($_GET[‘auswahl’]) )
{
$bedrooms = explode(‘,’, $_GET[‘auswahl’]);
//Add our meta query to the original meta queries
$meta_query[] = array(
‘key’ => ‘p_reihe’,
‘value’ => $bedrooms,
‘compare’ => ‘IN’,
);
$bathrooms = explode(‘,’, $_GET[‘bathrooms’]);
$meta_query[] = array(
‘key’ => ‘p_energieklasse’,
‘value’ => $bathrooms,
‘compare’ => ‘IN’,
);
}
// update the meta query args
$query->set(‘meta_query’, $meta_query);
// always return
return;
}
in archive.php:
<div id=”search-houses”>
<?php
$field = get_field_object(‘p_reihe’);
$values = isset($_GET[‘p_reihe’]) ? explode(‘,’, $_GET[‘p_reihe’]) : array();
?>
<?php endforeach; ?>
<?php
$field = get_field_object(‘p_energieklasse’);
$values = isset($_GET[‘p_energieklasse’]) ? explode(‘,’, $_GET[‘p_energieklasse’]) : array();
?>
<?php foreach( $field[‘choices’] as $choice_value => $choice_label ): ?>
<?php endforeach; ?>
</div>
<script type=”text/javascript”>
(function($) {
$(‘#search-houses’).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(‘http://www.jemaniblue.ch/kategorie/handbrausen/?auswahl=’ + vals);
console.log( vals );
});
})(jQuery);
</script>
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 🙂
I was able to fix my error with the checkboxes of field options by changing the field name to the field key like so (for both custom fields):
$field = get_field_object('field_54d4ec8875f85');
$field2 = get_field_object('field_54d4e73975f79');
I referenced this: http://www.advancedcustomfields.com/resources/get_field_object/
Now I just need to sort out how to change the javascript to apply both custom filters. I’ll report back if I figure it out.
Did you succeed with the javascript? Could you possibly share the code?
The topic ‘WP archive with two or more filter’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.