SO网友:G. Siganos
我必须实施以下措施:
更改管理UI以包括我的自定义筛选器
add_action(\'wp_enqueue_media\', function () {
wp_enqueue_script(\'media-library-taxonomy-filter\', get_stylesheet_directory_uri() . \'/assets/js/custom-media-filter.js\', array(\'media-editor\', \'media-views\'));
});
<在自定义媒体筛选器中。js我正在附加自定义选择HTML,每次更改自定义选择时,在URL中添加查询参数,并使用此参数和值重定向
jQuery(document).ready(function () {
jQuery(\'.filter-items\').append(\'<select class="form-select" aria-label="Custom filter document_category" id="page-document_category"><option>-Choose-</option><option value="document">Document</option><option value="template">Template</option></select>\');
jQuery(\'#page-document_category\').change(function (e) {
insertParam(\'document_category\', this.value)
});
// insert and redirect with new value
function insertParam(key, value) {
key = encodeURIComponent(key);
value = encodeURIComponent(value);
// remove all other parameters
var kvp = [];
kvp.push(key + \'=\' + value);
document.location.search = params;
}
});
<在我的函数中添加操作。php绑定到
pre_get_posts
add_action(\'pre_get_posts\', \'my_custom_filter\');
并创建自定义函数
my_custom_filter
仅当我的自定义筛选器发送到URL时才更改元查询
if (!function_exists(\'my_custom_filter\')) {
function my_custom_filter($wp_query_obj)
{
$url_query = explode(\'&\', $_SERVER[\'QUERY_STRING\']);
$params = array();
foreach ($url_query as $param) {
if (isset($param) && $param[0] !== \'\') {
// prevent notice on explode() if $param has no \'=\'
if (strpos($param, \'=\') === false) $param += \'=\';
list($name, $value) = explode(\'=\', $param, 2);
$params[urldecode($name)][] = urldecode($value);
urldecode_deep($params);
}
}
$found = false;
if (isset($params[\'document_category\'])) {
$meta_key = \'document_category\';
$meta_value = $params[\'document_category\'][0];
$found = true;
}
// here i can add more filters
// else if ($params[\'document_type\']) {
// $meta_key = \'document_type\';
// $meta_value = $params[\'document_type\'][0];
// $found = true;
// }
if ($found) {
$meta_query = array(
array(
\'key\' => $meta_key,
\'value\' => $meta_value,
\'compare\' => \'=\',
),
);
$wp_query_obj->set(\'meta_query\', $meta_query);
$wp_query_obj->set(\'orderby\', \'meta_value_num\');
$wp_query_obj->set(\'order\', \'ASC\');
}
return;
}
}