diff --git a/includes/artisans.php b/includes/artisans.php index 231e55d..3bd8427 100644 --- a/includes/artisans.php +++ b/includes/artisans.php @@ -124,3 +124,122 @@ function render_custom_chantier_box_content($post) echo 'Ajouter un chantier'; } } + + + +/* ---------------------------------------------------------------------- + GESTION DE LA COLONNE METIERS DANS LA LISTE DES POSTS ARTISANS + ------------------------------------------------------------------------*/ + +// AJOUT D'UNE COLONNE CUSTOM +function metiers_patrimoine_artisans_add_acf_posts_columns($columns) +{ + global $current_screen; + + // SUPPRIMER LA COLONNE 'date' + if (isset($columns['date'])) { + unset($columns['date']); + } + $new_admin_col_arrays = array_slice($columns, 0, 2, true) + array('metiers' => 'Métiers') + array_slice($columns, 2, count($columns) - 2, true); + return array_merge($new_admin_col_arrays); +} +add_filter('manage_artisans_posts_columns', 'metiers_patrimoine_artisans_add_acf_posts_columns'); + + +/* ---------------------------------------------------------------------- + GESTION DE LA VALEUR DE CHAQUE COLONNE + ------------------------------------------------------------------------*/ + +function metiers_patrimoine_artisans_handle_posts_custom_columns($column) +{ + + $post_id = get_the_ID(); + + if ($column == 'metiers') { + $artisan = get_field('artisan', $post_id); + $terms = get_the_terms($post_id, 'metiers'); + if ($terms) { + $parent_terms = array_filter($terms, function ($term) { + return $term->parent == 0; + }); + // if (empty($parent_terms)) { + // echo '
Aucun métier parent
'; + // return; + // } + echo '×
'; + } + } +} +add_action('manage_artisans_posts_custom_column', 'metiers_patrimoine_artisans_handle_posts_custom_columns', 10, 2); + + + +/* ------------------------------------------- + CREATION DU DROPDOWN SELECT AVEC LES OPTIONS + ---------------------------------------------*/ + +function metiers_patrimoine_filter_posts_per_metiers_declare_dropdown() +{ + global $pagenow; + $post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post'; + + //only add filter to post type you want + if ($post_type == 'artisans') { + $metiers_by_parent = getAllMetiersTermsByParents(); + +?> + +query_vars['tax_query'] = array( + array( + 'taxonomy' => 'metiers', + 'field' => 'slug', + 'terms' => $_GET['metiers'], + 'include_children' => true, + ), + ); + } +} + +add_filter('parse_query', 'metiers_patrimoine_filter_post_by_metiers_query'); diff --git a/includes/chantiers.php b/includes/chantiers.php index 9793b4e..9f47a0d 100644 --- a/includes/chantiers.php +++ b/includes/chantiers.php @@ -1,8 +1,8 @@ 'Artisan') + array('date_chantier' => 'Date chantier') + array_slice($columns, 2, count($columns) - 2, true); + return array_merge($new_admin_col_arrays); +} +add_filter('manage_chantiers_posts_columns', 'metiers_patrimoine_add_acf_posts_columns'); + + +/* ---------------------------------------------------------------------- + GESTION DE LA VALEUR DE CHAQUE COLONNE + ------------------------------------------------------------------------*/ + +function metiers_patrimoine_handle_posts_custom_columns($column) +{ + $post_id = get_the_ID(); + + if ($column == 'artisan') { + $artisan = get_field('artisan', $post_id); + if ($artisan) { + $edit_link = esc_url(get_edit_post_link($artisan->ID)); + echo ''; + echo esc_html($artisan->post_title); + echo ''; + } + } + if ($column == 'date_chantier') { + $date_chantier = get_field('date', $post_id); + echo '' . $date_chantier . '
'; + } +} +add_action('manage_chantiers_posts_custom_column', 'metiers_patrimoine_handle_posts_custom_columns', 10, 2); + + + + +function metiers_patrimoine_sortable_columns($columns) +{ + $columns['date_chantier'] = 'date_chantier'; + return $columns; +} +add_filter('manage_edit-chantiers_sortable_columns', 'metiers_patrimoine_sortable_columns'); + + +function metiers_patrimoine_orderby($query) +{ + if (!is_admin()) { + return; + } + + $orderby = $query->get('orderby'); + + if ('date_chantier' == $orderby) { + $query->set('meta_key', 'date'); + $query->set('orderby', 'meta_value'); + } +} +add_action('pre_get_posts', 'metiers_patrimoine_orderby');