La Compagnia del Tartufo

Some time ago I developed a custom wordpress solution for a client

The request was for a directory functionality that needed some tailoring. Client needed to be able to hierarchically define a custom taxonomy (geographical) and then show list pages

On top of this they also needed the possibility to sort results grouping them by type of structure (all restaurants, all hotels … ). Of course some bells and whistles were requested as well and this has been taken care of with the use of jquery and the map plugin to let the end user drill down the map

In this case I went for AdvancedCustomFields plugin together with some custom written code to enhance the plugin

If you take a look at this link

you will see a listing of two (at the time of my writing) cities and clicking on each name expands the relative children elements, sorted by structure type

In this case the grouping has children (Veneto/ contains Venezia/ and Cortina/ ) so the request was to show all the results with this accordion-like approach.

If you instead go to a grouping that has no children ( for instance) you will see all results already visible as soon as the page loads.

This effect has been implemented with a simple hack: first of all I query for the custom taxonomy term that’s currently queried and all the structure types available (I will need those in a moment)

$tipologie = get_field_object($tipologia_meta_name);
$args = array(
  'taxonomy'     => 'aziende',
  'orderby' => 'name',
  'order' => 'DESC',
  'term' => get_query_var('aziende')
$categories = get_categories($args); 

Then I check if there are children terms for that term:

$subargs = array(
	'taxonomy'     => 'aziende',
	'orderby' => 'name',
	'order' => 'DESC',
	'hide_empty' =>0,
	'parent' => $category->cat_ID, 
	$subcat = get_categories($subargs);

if yes I print out the terms for grouping and query for possible content falling under that taxonomy:

foreach($tipologie['choices'] as $k => $v){
		'aziende' => esc_sql(str_replace(" ","_",$sub->name)),
		'post_type' => 'azienda',
		'post_status' => 'publish',
		'posts_per_page' => -1,
		'caller_get_posts'=> 1,
		'orderby' => 'meta_value_num',
		'order' => 'DESC',
 		'meta_query' => array(
				'key' => $tipologia_meta_name,
				'value' => $v,
				'compare' => 'LIKE',
		$my_query = null;
                $my_query = new WP_Query($args);

and subsequently I do the usual wp stuff

Same goes for the case where the queried term has not child terms, I just varied the relative markup slightly

It’s been fast and quite fun implementing this feature.
The only problem I ran into was with a corner case regarding the ACF plugin and the creation of new fields when the database already contains posts.
Even if you specify a default value the ACF plugin doesn’t “retro-fit” the change so you may have posts that don’t have that particular field.
I wrote about this in a blog post some time ago

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *