Admin: Wie man eine benutzerdefinierte Listenfilter-Schaltfläche erstellt, sendet GET-Abfragevars

Ich habe benutzerdefinierte Spalten für einen benutzerdefinierten Post-Typ festgelegt, und ich möchte eine Filterfunktion hinzufügen. Ich muss benutzerdefinierte Daten verwenden, also kann ich nicht von Dingen wie wp_dropdown_categories und dergleichen profitieren.

Mein aktueller Code:

// add select function add_filter_by_macroarea() { global $typenow; $post_type = 'distributor'; $macroaree = array('italy','france','spain','world'); if($typenow == $post_type) { $html = ''; foreach($macroaree as $macroarea){ $html .= '' . $macroarea . ''; } $html .= ''; echo $html; }; } add_action('restrict_manage_posts', 'add_filter_by_macroarea'); // add filtered contents function convert_id_to_term_in_query($query){ global $pagenow; $post_type = 'distributor'; $q_vars = &$query->query_vars; // stop, not finished var_dump($q_vars); // just to see query vars, not already setted } add_filter('parse_query', 'convert_id_to_term_in_query'); 

Jetzt bin ich an dem Punkt, ich muss eine benutzerdefinierte GET-Abfragevar, die Filterauswahl lesen. Sagen wir es heißt “Makrobereich” und die Werte sollten “Italien”, “Frankreich”, “Spanien” oder “Welt” sein.

Mein Problem ist: Wie kann ich eine solche benutzerdefinierte Abfragevar übergeben?

Solutions Collecting From Web of "Admin: Wie man eine benutzerdefinierte Listenfilter-Schaltfläche erstellt, sendet GET-Abfragevars"

Dank @cbmeta konnte ich mein Problem lösen. Im Folgenden poste ich den ganzen relevanten Code. Denken Sie daran, dass es sich um einen benutzerdefinierten Beitragstyp “Verteiler” handelt, der 4 benutzerdefinierte Taxonomien verwendet (“Italien”, “Frankreich”, “Spanien”, “Welt”).

 // add columns function add_distributor_columns($table_columns){ $area_column = array('area' => 'Area'); $macroarea_column = array('macroarea' => 'Macroarea'); // put them on the right position $table_columns = array_slice($table_columns, 0, 2, true) + $area_column + $macroarea_column + array_slice($table_columns, 2, NULL, true); return $table_columns; } add_filter('manage_distributor_posts_columns', 'add_distributor_columns', 10); // add contents to columns function add_contents_to_distributor_columns($column_name, $id){ global $post; $post_id = $post->ID; if($column_name == 'area'){ $italy_areas = (array) get_the_terms($post_id,'italy'); $france_areas = (array) get_the_terms($post_id,'france'); $spain_areas = (array) get_the_terms($post_id,'spain'); $world_areas = (array) get_the_terms($post_id,'world'); $areas = $italy_areas + $france_areas + $spain_areas + $world_areas; if(!empty($areas) && $areas[0] !== false){ $areas_names = array(); foreach($areas as $area){ $area_name = $area->name; $areas_names[] = $area_name; } echo implode(', ',$areas_names); } }//end if if($column_name == 'macroarea'){ $macroareas = array(); if(has_taxonomy('italy', $post_id)) $macroareas[] = 'Italia'; if(has_taxonomy('france', $post_id)) $macroareas[] = 'Francia'; if(has_taxonomy('spain', $post_id)) $macroareas[] = 'Spagna'; if(has_taxonomy('world', $post_id)) $macroareas[] = 'Mondo'; if(!empty($macroareas)) echo implode(', ',$macroareas); }//end if }// add_contents_to_distributor_columns add_action('manage_distributor_posts_custom_column', 'add_contents_to_distributor_columns', 10, 2); // add macroarea filter function add_filter_by_macroarea() { global $typenow; $post_type = 'distributor'; $macroaree = array('all','italy','france','spain','world'); $selected = isset($_GET['macroarea']) ? $_GET['macroarea'] : ''; if($typenow == $post_type) { $html = ''; }; } add_action('restrict_manage_posts', 'add_filter_by_macroarea'); // add contents to filtered list function convert_id_to_term_in_query($query){ global $pagenow, $post_type; if($pagenow == 'edit.php' && $post_type == 'distributor'){ $selected = isset($_GET['macroarea']) ? $_GET['macroarea'] : null; if($selected && $selected != 'all'){ $terms_ids = wp_list_pluck(get_terms($selected), 'term_id'); $query->query_vars['tax_query'] = array( array( 'taxonomy' => $selected, 'field' => 'term_id', 'terms' => $terms_ids, ) ); }// if selected }// if right page }// end filter add_filter('parse_query', 'convert_id_to_term_in_query'); // UTILITY FUNCTIONS ---------- // function get_taxonomies_info($post_id = null){ if(!$post_id){ // if not passed, try to get the ID global $wp_query; $post_id = $wp_query->get_queried_object_id();//current post id } global $wpdb; return $wpdb->get_results($wpdb->prepare(" select tax.taxonomy as taxonomy, group_concat(tr.name) as term_names, group_concat(tr.term_id) as term_ids from " . $wpdb->prefix . "term_taxonomy tax INNER JOIN " . $wpdb->prefix . "term_relationships rel ON tax.term_taxonomy_id =rel.term_taxonomy_id INNER JOIN " . $wpdb->prefix . "terms tr ON tr.term_id = tax.term_id WHERE rel.object_id = %d GROUP BY taxonomy ",$post_id),OBJECT_K); } function has_taxonomy($taxonomy_slug, $post_id = null){ if(!$post_id){// if not passed, try to get the ID global $wp_query; $post_id = $wp_query->get_queried_object_id();//current post id } return array_key_exists($taxonomy_slug, get_taxonomies_info($post_id)); }