handling custom columns
This commit is contained in:
parent
10ae3a6f99
commit
64da1c65dd
|
|
@ -124,3 +124,122 @@ function render_custom_chantier_box_content($post)
|
|||
echo '<a href="' . admin_url('post-new.php?post_type=chantiers') . '" class="new-chantier">Ajouter un chantier</a>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
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 '<p class="no-results">Aucun métier parent</p>';
|
||||
// return;
|
||||
// }
|
||||
echo '<div class="admin-column-metiers-container">';
|
||||
foreach ($terms as $term) {
|
||||
// write_log($term);
|
||||
echo '<a href="' . get_edit_term_link($term->term_id, 'metiers', 'artisans') . '" class="admin-column-taxonomy-term">';
|
||||
echo esc_html($term->name);
|
||||
echo '</a>';
|
||||
}
|
||||
echo '</div >';
|
||||
} else {
|
||||
echo '<p class="no-results"> × </p>';
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
|
||||
?>
|
||||
<select name="metiers">
|
||||
<option value=""><?php _e('Filtrer par Métier', 'metiers-patrimoine-theme'); ?></option>
|
||||
<?php
|
||||
$current_v = isset($_GET['metiers']) ? $_GET['metiers'] : '';
|
||||
foreach ($metiers_by_parent as $index => $parent_term_data) {
|
||||
$parent_term = $parent_term_data['term'];
|
||||
|
||||
printf(
|
||||
'<option value="%s"%s>%s</option>',
|
||||
$parent_term->slug,
|
||||
$parent_term->slug == $current_v ? ' selected="selected"' : '',
|
||||
$parent_term->name
|
||||
);
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
add_action('restrict_manage_posts', 'metiers_patrimoine_filter_posts_per_metiers_declare_dropdown');
|
||||
|
||||
|
||||
/* ------------------------------------------------------
|
||||
FILTRAGE DES POSTS QUAND LA QUERY DE FILTER EST ENVOYEE
|
||||
--------------------------------------------------------*/
|
||||
|
||||
function metiers_patrimoine_filter_post_by_metiers_query($query)
|
||||
{
|
||||
global $pagenow;
|
||||
$type = 'artisans'; // change to custom post name.
|
||||
if (isset($_GET['post_type'])) {
|
||||
$type = $_GET['post_type'];
|
||||
}
|
||||
if (($type === 'artisans') && is_admin() && $pagenow == 'edit.php' && isset($_GET['metiers']) && $_GET['metiers'] != '') {
|
||||
$query->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');
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
<?php
|
||||
|
||||
/* ---------------------------
|
||||
EDIT ARTISAN LINK
|
||||
---------------------------*/
|
||||
/* -----------------------------------------
|
||||
EDIT ARTISAN LINK ON CHANTIER EDIT PAGE
|
||||
-----------------------------------------*/
|
||||
|
||||
add_action('acf/render_field', function ($field) {
|
||||
// Vérifiez si le champ est celui que vous voulez personnaliser
|
||||
|
|
@ -20,3 +20,74 @@ add_action('acf/render_field', function ($field) {
|
|||
}
|
||||
}
|
||||
}, 10, 1);
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
GESTION DE LA COLONNE ARTISAN DANS LA LISTE DES POSTS CHANTIERS
|
||||
------------------------------------------------------------------------*/
|
||||
|
||||
// AJOUT D'UNE COLONNE CUSTOM
|
||||
function metiers_patrimoine_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('artisan' => '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 '<a href="' . $edit_link . '">';
|
||||
echo esc_html($artisan->post_title);
|
||||
echo '</a>';
|
||||
}
|
||||
}
|
||||
if ($column == 'date_chantier') {
|
||||
$date_chantier = get_field('date', $post_id);
|
||||
echo '<p>' . $date_chantier . '</p>';
|
||||
}
|
||||
}
|
||||
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');
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user