Fix filtering and columns acf
This commit is contained in:
parent
eb4830a478
commit
65da02dd62
|
|
@ -35,20 +35,36 @@ add_action('acf/save_post', 'save_acf_post_mdp_status');
|
||||||
function metiers_patrimoine_artisans_add_acf_posts_columns($columns)
|
function metiers_patrimoine_artisans_add_acf_posts_columns($columns)
|
||||||
{
|
{
|
||||||
global $current_screen;
|
global $current_screen;
|
||||||
|
$my_current_lang = apply_filters('wpml_current_language', NULL);
|
||||||
|
|
||||||
// SUPPRIMER LA COLONNE 'date'
|
// SUPPRIMER LA COLONNE 'date'
|
||||||
if (isset($columns['date'])) {
|
if (isset($columns['date'])) {
|
||||||
unset($columns['date']);
|
unset($columns['date']);
|
||||||
}
|
}
|
||||||
$customColumns = array(
|
|
||||||
'metiers' => 'Métiers',
|
if($my_current_lang == 'nl'):
|
||||||
'elements' => 'Éléments du bâtiment',
|
$customColumns = array(
|
||||||
'conseiller' => 'Conseiller',
|
'metiers' => 'Métiers',
|
||||||
'state' => 'État',
|
//'elements' => 'Éléments du bâtiment',
|
||||||
'mdpstatus' => 'Statut de travail',
|
'conseiller' => 'Conseiller',
|
||||||
'lastmodified' => 'Dernière modification',
|
'state' => 'État',
|
||||||
'onlinedate' => 'Mise en ligne'
|
'lastmodified' => 'Dernière modification',
|
||||||
);
|
'onlinedate' => 'Mise en ligne',
|
||||||
|
'remark' => 'Remarque'
|
||||||
|
);
|
||||||
|
else:
|
||||||
|
$customColumns = array(
|
||||||
|
'metiers' => 'Métiers',
|
||||||
|
//'elements' => 'Éléments du bâtiment',
|
||||||
|
'conseiller' => 'Conseiller',
|
||||||
|
'state' => 'État',
|
||||||
|
'mdpstatus' => 'Statut de travail',
|
||||||
|
'lastmodified' => 'Dernière modification',
|
||||||
|
'onlinedate' => 'Mise en ligne',
|
||||||
|
'remark' => 'Remarque'
|
||||||
|
);
|
||||||
|
endif;
|
||||||
|
|
||||||
$new_admin_col_arrays = array_slice($columns, 0, 2, true) + $customColumns + array_slice($columns, 2, count($columns) - 2, true);
|
$new_admin_col_arrays = array_slice($columns, 0, 2, true) + $customColumns + array_slice($columns, 2, count($columns) - 2, true);
|
||||||
return array_merge($new_admin_col_arrays);
|
return array_merge($new_admin_col_arrays);
|
||||||
}
|
}
|
||||||
|
|
@ -59,9 +75,9 @@ add_filter('manage_artisans_posts_columns', 'metiers_patrimoine_artisans_add_acf
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
GESTION DE LA VALEUR DE CHAQUE COLONNE
|
GESTION DE LA VALEUR DE CHAQUE COLONNE
|
||||||
------------------------------------------------------------------------*/
|
------------------------------------------------------------------------*/
|
||||||
function metiers_patrimoine_artisans_handle_posts_custom_columns($column)
|
function metiers_patrimoine_artisans_handle_posts_custom_columns($column, $post_id)
|
||||||
{
|
{
|
||||||
$post_id = get_the_ID();
|
//$post_id = get_the_ID();
|
||||||
|
|
||||||
if ($column == 'conseiller') {
|
if ($column == 'conseiller') {
|
||||||
$conseiller = get_field('conseiller', $post_id);
|
$conseiller = get_field('conseiller', $post_id);
|
||||||
|
|
@ -106,24 +122,24 @@ function metiers_patrimoine_artisans_handle_posts_custom_columns($column)
|
||||||
echo '<p class="no-results"> × </p>';
|
echo '<p class="no-results"> × </p>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($column == 'elements') {
|
// if ($column == 'elements') {
|
||||||
$terms = get_the_terms_organised_by_parent('elementsbatiments', $post_id);
|
// $terms = get_the_terms_organised_by_parent('elementsbatiments', $post_id);
|
||||||
if ($terms) {
|
// if ($terms) {
|
||||||
$parent_terms = array_filter($terms, function ($term) {
|
// $parent_terms = array_filter($terms, function ($term) {
|
||||||
return $term->parent == 0;
|
// return $term->parent == 0;
|
||||||
});
|
// });
|
||||||
|
|
||||||
echo '<div class="admin-column-metiers-container">';
|
// echo '<div class="admin-column-metiers-container">';
|
||||||
foreach ($terms as $term) {
|
// foreach ($terms as $term) {
|
||||||
echo '<a href="' . get_edit_term_link($term->term_id, 'metiers', 'artisans') . '" class="admin-column-taxonomy-term">';
|
// echo '<a href="' . get_edit_term_link($term->term_id, 'metiers', 'artisans') . '" class="admin-column-taxonomy-term">';
|
||||||
echo esc_html($term->name);
|
// echo esc_html($term->name);
|
||||||
echo '</a>';
|
// echo '</a>';
|
||||||
}
|
// }
|
||||||
echo '</div >';
|
// echo '</div >';
|
||||||
} else {
|
// } else {
|
||||||
echo '<p class="no-results"> × </p>';
|
// echo '<p class="no-results"> × </p>';
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if ($column == 'lastmodified') {
|
if ($column == 'lastmodified') {
|
||||||
// $last_modified_date = get_the_modified_date('j F Y \à H:i', $post_id);
|
// $last_modified_date = get_the_modified_date('j F Y \à H:i', $post_id);
|
||||||
|
|
@ -136,9 +152,72 @@ function metiers_patrimoine_artisans_handle_posts_custom_columns($column)
|
||||||
if (!$published_date) return;
|
if (!$published_date) return;
|
||||||
echo $published_date;
|
echo $published_date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($column == 'remark') {
|
||||||
|
$last_action = get_field('last_action', $post_id);
|
||||||
|
//write_log($post_id);
|
||||||
|
//write_log($last_action);
|
||||||
|
|
||||||
|
|
||||||
|
if (!isset($last_action) || empty($last_action)) return; $remark = $last_action['comments'];
|
||||||
|
|
||||||
|
if (!isset($remark) || empty($remark)) return;
|
||||||
|
|
||||||
|
// $conseillerDatas = get_userdata($conseiller);
|
||||||
|
// if (!isset($conseillerDatas->display_name)) return;
|
||||||
|
|
||||||
|
echo $remark;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
add_action('manage_artisans_posts_custom_column', 'metiers_patrimoine_artisans_handle_posts_custom_columns', 10, 2);
|
add_action('manage_artisans_posts_custom_column', 'metiers_patrimoine_artisans_handle_posts_custom_columns', 10, 2);
|
||||||
|
|
||||||
|
/* ------
|
||||||
|
// Fonction pour charger les posts avec leurs ACF
|
||||||
|
-------*/
|
||||||
|
|
||||||
|
function load_posts_with_acf() {
|
||||||
|
// Vérifie si ACF est activé
|
||||||
|
if ( ! function_exists('get_field') ) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Récupère les posts (exemple : tous les articles publiés)
|
||||||
|
$posts_query = new WP_Query([
|
||||||
|
'post_type' => 'artisans', // Change selon ton besoin (page, custom post type...)
|
||||||
|
'posts_per_page' => -1, // Tous les posts
|
||||||
|
]);
|
||||||
|
|
||||||
|
$posts_data = [];
|
||||||
|
|
||||||
|
if ( $posts_query->have_posts() ) {
|
||||||
|
while ( $posts_query->have_posts() ) {
|
||||||
|
$posts_query->the_post();
|
||||||
|
$post_id = get_the_ID();
|
||||||
|
|
||||||
|
// Récupère les champs ACF (automatique ou spécifiques)
|
||||||
|
//$acf_fields = get_fields($post_id); // Renvoie tous les champs ACF du post
|
||||||
|
|
||||||
|
// Structure des données
|
||||||
|
$posts_data[$post_id] = [
|
||||||
|
'id' => $post_id,
|
||||||
|
'acf' => [
|
||||||
|
'conseiller' => get_field('conseiller', $post_id),
|
||||||
|
'mdp_status' => get_field('mdp_status', $post_id),
|
||||||
|
'last_action' => get_field('last_action', $post_id),
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
wp_reset_postdata();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $posts_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Variable globale accessible partout
|
||||||
|
global $global_posts_with_acf;
|
||||||
|
$global_posts_with_acf = load_posts_with_acf();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
GESTION DU FILTRAGE
|
GESTION DU FILTRAGE
|
||||||
|
|
@ -148,6 +227,9 @@ function metiers_patrimoine_filter_posts_declare_dropdowns()
|
||||||
{
|
{
|
||||||
global $typenow;
|
global $typenow;
|
||||||
global $wp_meta_boxes;
|
global $wp_meta_boxes;
|
||||||
|
global $global_posts_with_acf;
|
||||||
|
|
||||||
|
//write_log($global_posts_with_acf);
|
||||||
|
|
||||||
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post';
|
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post';
|
||||||
if ($post_type !== 'artisans') return;
|
if ($post_type !== 'artisans') return;
|
||||||
|
|
@ -159,25 +241,24 @@ function metiers_patrimoine_filter_posts_declare_dropdowns()
|
||||||
|
|
||||||
$mdp_status_choices = isset($mdp_status_datas['choices']) ? $mdp_status_datas['choices'] : null;
|
$mdp_status_choices = isset($mdp_status_datas['choices']) ? $mdp_status_datas['choices'] : null;
|
||||||
|
|
||||||
|
// $meta_key = 'mdp_status';
|
||||||
|
// $values = get_posts([
|
||||||
|
// 'post_status' => 'any',
|
||||||
|
// 'post_type' => 'artisans',
|
||||||
|
// 'posts_per_page' => -1,
|
||||||
|
// 'meta_key' => $meta_key,
|
||||||
|
// 'fields' => 'ids',
|
||||||
|
// ]);
|
||||||
|
|
||||||
$meta_key = 'mdp_status';
|
// $unique_values = array_reduce(array_filter(array_map(
|
||||||
$values = get_posts([
|
// fn($post_id) => get_field('mdp_status', $post_id) ?? null,
|
||||||
'post_status' => 'any',
|
// $values
|
||||||
'post_type' => 'artisans',
|
// )), function ($acc, $item) {
|
||||||
'posts_per_page' => -1,
|
// if (isset($item['value'], $item['label'])) {
|
||||||
'meta_key' => $meta_key,
|
// $acc[$item['value']] = $item['label'];
|
||||||
'fields' => 'ids',
|
// }
|
||||||
]);
|
// return $acc;
|
||||||
|
// }, []);
|
||||||
$unique_values = array_reduce(array_filter(array_map(
|
|
||||||
fn($post_id) => get_field('mdp_status', $post_id) ?? null,
|
|
||||||
$values
|
|
||||||
)), function ($acc, $item) {
|
|
||||||
if (isset($item['value'], $item['label'])) {
|
|
||||||
$acc[$item['value']] = $item['label'];
|
|
||||||
}
|
|
||||||
return $acc;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
<select name="metiers">
|
<select name="metiers">
|
||||||
|
|
@ -197,22 +278,22 @@ function metiers_patrimoine_filter_posts_declare_dropdowns()
|
||||||
?>
|
?>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select name="elementsbatiments">
|
<!-- <select name="elementsbatiments">
|
||||||
<option value="" class="please-select"><?php _e('Éléments du Bâtiment', 'metiers-patrimoine-theme'); ?></option>
|
<option value="" class="please-select"><?php _e('Éléments du Bâtiment', 'metiers-patrimoine-theme'); ?></option>
|
||||||
<?php
|
<?php
|
||||||
$is_current = isset($_GET['elementsbatiments']) ? $_GET['elementsbatiments'] : '';
|
// $is_current = isset($_GET['elementsbatiments']) ? $_GET['elementsbatiments'] : '';
|
||||||
foreach ($batiments_by_parent as $index => $parent_term_data) {
|
// foreach ($batiments_by_parent as $index => $parent_term_data) {
|
||||||
$parent_term = $parent_term_data['term'];
|
// $parent_term = $parent_term_data['term'];
|
||||||
|
|
||||||
printf(
|
// printf(
|
||||||
'<option value="%s"%s>%s</option>',
|
// '<option value="%s"%s>%s</option>',
|
||||||
$parent_term->slug,
|
// $parent_term->slug,
|
||||||
$parent_term->slug == $is_current ? ' selected="selected"' : '',
|
// $parent_term->slug == $is_current ? ' selected="selected"' : '',
|
||||||
$parent_term->name
|
// $parent_term->name
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
?>
|
?>
|
||||||
</select>
|
</select> -->
|
||||||
|
|
||||||
<select name="conseiller">
|
<select name="conseiller">
|
||||||
<option value="" class="please-select"><?php _e('Conseiller', 'metiers-patrimoine-theme'); ?></option>
|
<option value="" class="please-select"><?php _e('Conseiller', 'metiers-patrimoine-theme'); ?></option>
|
||||||
|
|
@ -238,8 +319,18 @@ function metiers_patrimoine_filter_posts_declare_dropdowns()
|
||||||
<option value="" class="please-select"><?php _e('Statut de travail', 'metiers-patrimoine-theme'); ?></option>
|
<option value="" class="please-select"><?php _e('Statut de travail', 'metiers-patrimoine-theme'); ?></option>
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
//write_log($unique_values);
|
||||||
|
$statuses = [
|
||||||
|
'rejected' => 'Non sélectionné (hors ligne)',
|
||||||
|
'to_contact' => 'Nouveau à contacter (Hors ligne)',
|
||||||
|
'to_actualize' => 'À actualiser (en ligne)',
|
||||||
|
'ok' => 'OK (en ligne)',
|
||||||
|
'none' => '—— Pas de statut !',
|
||||||
|
'deleted' => 'Supprimé (hors ligne)'
|
||||||
|
];
|
||||||
|
|
||||||
$is_current = isset($_GET['mdpstatus']) ? $_GET['mdpstatus'] : '';
|
$is_current = isset($_GET['mdpstatus']) ? $_GET['mdpstatus'] : '';
|
||||||
foreach ($unique_values as $index => $status) {
|
foreach ($statuses as $index => $status) {
|
||||||
printf(
|
printf(
|
||||||
'<option value="%s"%s>%s</option>',
|
'<option value="%s"%s>%s</option>',
|
||||||
$index,
|
$index,
|
||||||
|
|
@ -272,7 +363,7 @@ function filter_posts_by_acf_conseiller($query)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FILTRE CONSEILLER
|
// FILTRE CONSEILLER
|
||||||
if (!empty($_GET['mdpstatus'])) {
|
if (!empty($_GET['mdpstatus']) && empty($_GET['conseiller'])) {
|
||||||
$query->set('meta_query', [
|
$query->set('meta_query', [
|
||||||
[
|
[
|
||||||
'key' => 'mdp_status',
|
'key' => 'mdp_status',
|
||||||
|
|
@ -283,7 +374,7 @@ function filter_posts_by_acf_conseiller($query)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FILTRE CONSEILLER
|
// FILTRE CONSEILLER
|
||||||
if (!empty($_GET['conseiller'])) {
|
if (!empty($_GET['conseiller']) && empty($_GET['mdpstatus'])) {
|
||||||
|
|
||||||
if ($_GET['conseiller'] == 'non_attribue') {
|
if ($_GET['conseiller'] == 'non_attribue') {
|
||||||
$query->set('meta_query', [
|
$query->set('meta_query', [
|
||||||
|
|
@ -303,6 +394,25 @@ function filter_posts_by_acf_conseiller($query)
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($_GET['mdpstatus']) && !empty($_GET['conseiller'])) {
|
||||||
|
if ($_GET['conseiller'] == 'non_attribue'){
|
||||||
|
$_GET['conseiller'] = '';
|
||||||
|
}
|
||||||
|
$query->set('meta_query', [
|
||||||
|
[
|
||||||
|
'key' => 'mdp_status',
|
||||||
|
'value' => sanitize_text_field($_GET['mdpstatus']),
|
||||||
|
'compare' => '='
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'key' => 'conseiller',
|
||||||
|
'value' => sanitize_text_field($_GET['conseiller']),
|
||||||
|
'compare' => '='
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add_action('pre_get_posts', 'filter_posts_by_acf_conseiller');
|
add_action('pre_get_posts', 'filter_posts_by_acf_conseiller');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user