LR
← Back to projects

Notable Alumni (filterable module, WP + WPML).

'Notable Alumni' module filterable by taxonomy on WP + WPML: dynamic fetch/AJAX grid, state persisted via URL hash, FR/EN multilingual.

wordpressphpwp_querywpmljavascriptfetch/ajaxcss
Notable Alumni (filterable module, WP + WPML)

Challenges

  1. 01.Create a taxonomy filter with dynamic loading (without reloading the page)
  2. 02.Correctly handle multilingual (WPML): translated terms + consistent queries
  3. 03.Guarantee clean rendering even when some fields are empty (photo, bio, company, etc.)
  4. 04.Sort results reliably (alphabetical order by name)

Solutions

  1. 01.PHP endpoint returning alumni as JSON via WP_Query (CPT + tax_query)
  2. 02.WPML language switch on the endpoint + fetching the translated term_id to filter correctly
  3. 03.Front-end rendering in JS (fetch) with a display fallback when a field is missing
  4. 04.Filter persistence via the URL hash to keep state on refresh / link sharing
AJAX endpoint: WPML + tax_query + JSONphp
1<?php
2require_once('../../../wp-load.php');
3header('Content-Type: application/json');
4
5$lang = isset($_GET['lang']) ? sanitize_text_field($_GET['lang']) : 'fr';
6$category = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
7
8do_action('wpml_switch_language', $lang);
9
10$args = [
11 'post_type' => 'notable-alumnus',
12 'posts_per_page' => -1,
13 'post_status' => 'publish',
14 'orderby' => 'title',
15 'order' => 'ASC',
16];
17
18// Filtre par catégorie (taxonomie) avec traduction WPML
19if (!empty($category)) {
20 $term = get_term_by('slug', $category, 'notable-alumni-category');
21 if ($term && function_exists('apply_filters')) {
22 $translated_term_id = apply_filters('wpml_object_id', $term->term_id, 'notable-alumni-category', true, $lang);
23 if ($translated_term_id) {
24 $args['tax_query'] = [[
25 'taxonomy' => 'notable-alumni-category',
26 'field' => 'term_id',
27 'terms' => $translated_term_id,
28 ]];
29 }
30 }
31}
32
33$q = new WP_Query($args);
34$out = [];
35
36while ($q->have_posts()) {
37 $q->the_post();
38 $id = get_the_ID();
39
40 $out[] = [
41 'title' => get_the_title(),
42 'thumbnail' => get_the_post_thumbnail_url($id, 'full'),
43 'poste' => get_post_meta($id, 'wpcf-poste', true),
44 'societe' => get_post_meta($id, 'wpcf-societe', true),
45 'annee' => get_post_meta($id, 'wpcf-annee-diplomation', true),
46 'bio_url' => get_post_meta($id, 'wpcf-biographie', true),
47 'categories'=> wp_get_post_terms($id, 'notable-alumni-category', ['fields' => 'names']),
48 ];
49}
50
51wp_reset_postdata();
52echo json_encode($out);
53exit;
→ NextOther projects
Notable Alumni (filterable module, WP + WPML) | Louis Rotellini