Home › Forums › Front-end Issues › Creating a WP archive with custom field filter error
Hello,
I follow this documentation:
https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
Here is the code:
####################
// array of filters (field key => field name)
$GLOBALS[‘my_query_filters’] = array(
‘field_1’ => ‘couloc’,
‘field_2’ => ‘coucat’
);
// action
add_action(‘pre_get_posts’, ‘my_pre_get_posts’, 10, 1);
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) return;
// bail early if not main query
// – allows custom code / plugins to continue working
if( !$query->is_main_query() ) 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);
}
####################
But when I try I have an error… (see image)
Can you tell me why?
You have a fatal error in your code which I cannot see by looking at it.
You need to disable WP WSOD protection https://wordpress.stackexchange.com/questions/337391/how-to-disable-the-fatal-error-wsod-protection
And you need to enable debugging https://codex.wordpress.org/WP_DEBUG
so that you can see what the real error is.
ok I found a solution..
I had to had this..
$meta_query = [];
before
// append meta query
$meta_query[] = array(
‘key’ => $name,
‘value’ => $value,
‘compare’ => ‘IN’,
);
The topic ‘Creating a WP archive with custom field filter error’ 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.