过滤查询后如何设置选项上的选中属性?

时间:2017-09-06 作者:aberkow

我正在为一个自定义帖子类型开发一个管理过滤器,并且大部分都能正常工作。然而,我只停留在一点上。在筛选查询之后,如何使所选选项对用户可见?

我已经看过了wp_dropdown_categories (我在另一个过滤器中使用了它),它似乎不适合我的情况。在这种情况下,过滤器针对的是数据库查询。这就是我目前所拥有的。

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>";
  }
}
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;
  }
  return $query;
};
add_filter(\'parse_query\', \'query_posts_by_status\');

1 个回复
SO网友:aberkow

好吧,这不是解决这个问题的最好办法,但这就是我想到的。结合使用会话存储和少量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\');

结束