Home › Forums › General Issues › Parameters in url: array returns bool (false)
Hello,
I have used the following tutorial the set up custom field filtering:
https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
– I created a custom field (type: select) with name brand
– I’m using a normal post type
When I goto the category url I can see the filters:
http://demo.starrenburg.net/category/herenschoenen/
I also included a var_dump of the field
When I then select one of the checkboxes, the url gets update. However the checkboxes are not displayed anymore:
http://demo.starrenburg.net/category/herenschoenen/?brand=vanbommel
At this point I’m stuck and I don’t know what’s going wrong, any help would be much appreciated.
I have included the following code in functions.php
/*------------------------------------*\
ACF Custom field filter
\*------------------------------------*/
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'field_583503060dc55' => 'brand',
);
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) {
return;
}
// get meta query
$meta_query = $query->get('meta_query');
// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$value = explode(',', $_GET[ $name ]);
// append meta query
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
}
// update meta query
$query->set('meta_query', $meta_query);
}
// action
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
I also created a function in functions.php to call and display the filter
function filter_herenschoenen() {
$field = get_field_object('field_583503060dc55');
$values = explode(',', $_GET['field_583503060dc55']);
?>
<ul>
<?php foreach( $field['choices'] as $choice_value => $choice_label): ?>
<li>
<input type="checkbox" id="<?php echo $choice_value; ?>" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values)): ?> checked="checked" <?php endif; ?> />
<?php echo '<label for=' . $choice_value . '>' . $choice_label . '</label>' ; ?>
</li>
<?php endforeach; ?>
</ul>
<?php }
the code I’m using in category.php
<div id="search-herenschoenen">
<?php filter_herenschoenen(); ?>
<script type="text/javascript">
(function ($) {
$('#search-herenschoenen').on('change', 'input[type="checkbox"]', function(){
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('category/herenschoenen'); ?>?brand=' + vals);
console.log( vals );
});
})(jQuery);
</script>
</div>
Hi @michstar
I believe there’s something wrong with your filter_herenschoenen()
function. Could you please debug the $field
variable like the following?
var_dump($field);
Also, could you follow the example on that page like this instead:
<?php
function filter_herenschoenen() {
$field = get_field_object('field_583503060dc55', false, false);
$field['value'] = explode(',', $_GET['brand']);
?>
<div class="filter" data-filter="<?php echo 'brand'; ?>">
<?php create_field( $field ); ?>
</div>
<?php } ?>
Thanks 🙂
Hi James,
As you may notice, I’m new to php 🙂
Ok when you visit: http://demo.starrenburg.net/category/herenschoenen/
on this page I’m calling the function like mention above
<?php filter_herenschoenen(); ?>
I also include the var dumps:
<?php
$brand = get_field_object('field_583503060dc55');
echo '<pre>';
var_dump( $brand );
echo '</pre>';
?>
So when I var dump &brand without parameters in the url, I can see the array
however when there are parameters in the url the array returns false. (if I’m correct)
in the field config:
field_key: field_583503060dc55
field_name: brand
type: select
I will update the archive.php with the example code
Hi @michstar
In this case, I suggest you stick with the example instead. So instead of modifying the structure, you can try to move the code to a function like this:
function filter_herenschoenen() {
foreach( $GLOBALS['my_query_filters'] as $key => $name ):
// get the field's settings without attempting to load a value
$field = get_field_object($key, false, false);
// set value if available
if( isset($_GET[ $name ]) ) {
$field['value'] = explode(',', $_GET[ $name ]);
}
// create filter
?>
<div class="filter" data-filter="<?php echo $name; ?>">
<?php create_field( $field ); ?>
</div>
<?php endforeach;
}
Also, could you please share the JSON or XML export file of your field group, your functions.php file and your archive.php/category.php file? If the code is too long, you can use https://gist.github.com/.
Thanks 🙂
Hi @acf-support,
Thanks again for your reply. I have the select box now,
http://demo.starrenburg.net/category/herenschoenen/
but for some reason the url isn’t updated, I do make use of materializecss:
http://materializecss.com/forms.html#select-initialization
I have uploaded the files to:
https://gist.github.com/anonymous/ce84f3ecf0218f2ba02703efc56bde62
Hi @michstar
It seems you have a very different script to redirect the URL. Could you please stick with the example and start from there?
Also, could you please share the JSON or XML export file so I can test it out on my installation?
Thanks 🙂
The topic ‘Parameters in url: array returns bool (false)’ 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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.