1

UPDATED!!

So when I choose an option my admin table does not change based on filter

I have create code to make custom filter by custom postmeta. My filter is created and appeared above table but does not work when I click any option in dropdown filter.

Here the code to make a filter

    add_action('restrict_manage_posts', 'add_extra_tablenav');
function add_extra_tablenav($post_type){

    global $wpdb;

    /** Ensure this is the correct Post Type*/
    if($post_type !== 'screening')
        return;

    /** Grab the results from the DB */
    $query = $wpdb->prepare('
        SELECT DISTINCT pm.meta_value FROM %1$s pm
        LEFT JOIN %2$s p ON p.ID = pm.post_id
        WHERE pm.meta_key = "%3$s" 
        AND p.post_status = "%4$s" 
        AND p.post_type = "%5$s"
        ORDER BY "%3$s"',
        $wpdb->postmeta,
        $wpdb->posts,
        'screening_status', // Your meta key - change as required
        'publish',          // Post status - change as required
        $post_type
    );
    $results = $wpdb->get_col($query);

    /** Ensure there are options to show */
    if(empty($results))
        return;

    /** Grab all of the options that should be shown */
    $options[] = sprintf('<option value="-1">%1$s</option>', __('Filter by Status', 'your-text-domain'));
    foreach($results as $result) :
        $options[] = sprintf('<option value="%1$s">%2$s</option>', esc_attr($result), $result);
    endforeach;

    /** Output the dropdown menu */
    echo '<select class="" id="status-name" name="status-name">';
    echo join("\n", $options);
    echo '</select>';

}

and here my code to generate results based on filter's option

    add_filter('parse_query', 'prefix_status_filter');

function prefix_status_filter($query)
{
    global $pagenow;
    $current_page = isset($_GET['post_type']) ? $_GET['post_type'] : '';
    if (is_admin() && 'screening' == $current_page && 'edit.php' == $pagenow && isset($_GET['status-name']) && $_GET['status-name'] != '')
    {
        $analyst_name = $_GET['status-name'];
        $query->query_vars['meta_key'] = 'screening_status';
        $query->query_vars['meta_value'] = $result;
        $query->query_vars['meta_compare'] = '=';
    } //is_admin() && 'screening' == $current_page && 'edit.php' == $pagenow && isset($_GET['analysts-name']) && $_GET['analysts-name'] != ''
}

Please help ....!!!

  • Does not work is not especially helpful. Can you be more specific? – jdm2112 Mar 22 '18 at 2:59
  • @jdm2112 so when i choose option in filter my table does not change based on filter. – Muhammad Ibrahim Mar 22 '18 at 3:06
  • @jdm2112 this table in backend (admin table list) – Muhammad Ibrahim Mar 22 '18 at 3:51
  • are you including class-wp-list-table.php to extend a custom class or did you copy it into your plugin? – chop62 Mar 22 '18 at 4:06
  • @chop62 I copy into my plugin, because I have create custom post type and already generate admin table so i just add that code inside my plugin. is it my issue?? – Muhammad Ibrahim Mar 22 '18 at 4:26

Your Answer

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

Browse other questions tagged or ask your own question.