我在function.php中添加了一些代码来在我的WP站点中创建一些过滤器。 该脚本如下:
$GLOBALS['my_query_filters'] = array(
'field_1' => 'forma',
'field_2' => 'posa',
'field_3' => 'conduttore',
'field_4' => 'isolante',
'field_5' => 'raggio_minimo_di_curvatura',
'field_6' => 'guaina',
'field_7' => 'marchiatura',
'field_8' => 'tensione_di_esercizio',
'field_9' => 'temp_max_esercizio',
'field_10' => 'temp_min_di_inst',
'field_11' => 'temp_max_corto'
);
// 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;
}
// 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);
}
目前,此代码仅显示10个空行,但有任何复选框或过滤器名称。
你能帮助我吗?