好吧,这不是解决这个问题的最好办法,但这就是我想到的。结合使用会话存储和少量javascript。
第一个-我添加了session_start()
到我文件的开头。然后在query_posts_by_status
我补充道$_SESSION[\'request_status\'] = $request_status
. 这让我可以访问全球$_SESSION
变量最后,我添加了一点javascript,如下所示filter_forms_by_status
.
<script>
var statusFilter = document.getElementById(\'status-filter\');
var filter = "<?php echo $_SESSION[\'request_status\'] ?>";
statusFilter.value = filter;
</script>
所以,我对这个案子的完整解决方案是。。。
start_session(); // at top of file.
function filter_forms_by_status() {
global $post_type;
global $wpdb;
if ($post_type != \'request_form\') {
return;
} else {
$query = "
SELECT DISTINCT post_name
FROM $wpdb->posts
WHERE post_title LIKE \'request_status\'
";
$result = $wpdb->get_row($query, ARRAY_N);
$field_object = get_field_object($result[0]);
$status_array = $field_object[\'choices\'];
echo "<select class=\'postform\' id=\'status-filter\' name=\'status\'>";
foreach ($status_array as $status_key => $status) {
echo "<option value=$status_key>$status</option>";
}
echo "</select>";
}
?>
<script>
var statusFilter = document.getElementById(\'status-filter\');
var filter = "<?php echo $_SESSION[\'request_status\'] ?>";
statusFilter.value = filter;
</script>
<?php
}
add_action(\'restrict_manage_posts\', \'filter_forms_by_status\');
function query_posts_by_status($query) {
global $pagenow;
if (is_admin() && $pagenow == \'edit.php\') {
$request_status = $_GET[\'status\'];
$query->query_vars[\'meta_key\'] = \'request_status\';
$query->query_vars[\'meta_value\'] = $request_status;
$_SESSION[\'request_status\'] = $request_status;
}
return $query;
};
add_filter(\'parse_query\', \'query_posts_by_status\');