0

I am working on an admin filter for a custom post type and have it mostly working. However, I'm stuck on one point. How can I keep the selected option visible to the user after the filter query?

I've looked at wp_dropdown_categories (which I used in a different filter) and it doesn't seem to fit my case. In this case, the filter is against a database query. This is what I have so far.

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');
0

Ok - it's not the prettiest way to solve this, but this is what I came up with. Using a combination of session storage and a little javascript.

First - I added session_start() to the beginning of my file. Then in query_posts_by_status I added $_SESSION['request_status'] = $request_status. This gave me access to the global $_SESSION variable. Last, I added a little javascript as follows to filter_forms_by_status.

<script>
  var statusFilter = document.getElementById('status-filter');
  var filter = "<?php echo $_SESSION['request_status'] ?>";
  statusFilter.value = filter;
</script>

So, my complete solution for this case is...

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.