WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a table, I now need to filter the data from that table when someone selects options from a form.

Whats a good way to do that?

share|improve this question
up vote 1 down vote accepted

Here is the html:

<form name="filter" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
    <input type="hidden" name="filter_submit" value="Y">
        <select name="your_filter" class="input">
            <option value="none">nothing selected</option>
            <option value="b">b</option>
            <option value="c">c</option>                                                    
    <input type="submit" name="submit" class="input" value="Filter" />

Here is the php to filter data:

function filter() {
global $wpdb;
$table_name = $wpdb->prefix . "your_table_listings";
$sql = $wpdb->get_results("SELECT * FROM $table_name");

if ($_POST['filter_submit'] == 'Y'){
        $your_filter = $_POST['your_filter'];

        if ($your_filter !== 'none'){
                    $add_sql = ' yourcolumn = \'' . $your_filter . '\'';  
                    $sql = $wpdb->get_results("SELECT * FROM $table_name WHERE $add_sql");

Here are some links that might help:


share|improve this answer
eek, whatever you do, don't put POST data directly in a query like that! use prepared statements. – Milo Jun 9 '12 at 14:27

If you use $_POST and know the options you should check to make sure no additional data is coming in. Only the options.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.