custom filtering admin columns

i'm trying to add custom filters to a CPT job-offer, for example, i'm starting by locations. the filters works fine except when i try to filter by all locations.

this is my code

function add_custom_filter_tablenav($post_type){
   
   global $wpdb;
   
   /** Ensure this is the correct Post Type*/
   if($post_type !== 'job-offer')
       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 %6$s',
       $wpdb-postmeta,
       $wpdb-posts,
       'location', // Your meta key - change as required
       'publish',          // Post status - change as required
       $post_type,
       'location'
   );
   $results = $wpdb-get_col($query);
   
   /** Ensure there are options to show */
   if(empty($results))
       return;

   // get selected option if there is one selected
   if (isset( $_GET['location'] )  $_GET['location'] != '') {
       $selectedName = $_GET['location'];
   } else {
       $selectedName = -1;
   }
   
   /** Grab all of the options that should be shown */
   $options[] = sprintf('option value=-1%1$s/option', __('All Locations', 'textdomain'));
   foreach($results as $result) :
       if ($result == $selectedName) {
           $options[] = sprintf('option value=%1$s selected%2$s/option', esc_attr($result), $result);
       } else {
           $options[] = sprintf('option value=%1$s%2$s/option', esc_attr($result), $result);
       }
   endforeach;

   /** Output the dropdown menu */
   echo 'select class= id=location name=location';
   echo join(\n, $options);
   echo '/select';

}
add_filter( 'parse_query', 'prefix_parse_filter' );
function  prefix_parse_filter($query) {
  global $pagenow;
  $current_page = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
  
  if ( is_admin()  
    'job-offer' == $current_page 
    'edit.php' == $pagenow  
     isset( $_GET['location'] )  
     $_GET['location'] != '' ) {
  
   $location                  = $_GET['location'];
   $query-query_vars['meta_key']     = 'location';
   $query-query_vars['meta_value']   = $location;
   $query-query_vars['meta_compare'] = '=';
 }
}

Topic screen-columns columns admin-bar filters custom-post-types Wordpress

Category Web


I ended up changing the value when all Locations is selected.Now it's working perfectly.

$options[] = sprintf('<option value="">%1$s</option>', __('All Locations', 'textdomain'));``` 

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.