Hello,
I’m going to show you something interesting that I did.
I think Bricks should allow us to do the same thing without code, maybe as a function to activate inside settings, but it will require some deep thinking.
In any case, the code I created does its job. The only problem is you have to apply it before posting the post and make sure to save each post or if you have already posts re-save to apply.
// Hook into the 'save_post' action to trigger the 'assign_parent_terms' function when a post is saved.
add_action('save_post', 'assign_parent_terms', 10, 2);
function assign_parent_terms($post_id, $post) {
// Check if the post type is 'property'. If not, exit the function.
if($post->post_type != 'property') return $post_id;
// Get all terms assigned to the post in the 'property-city' taxonomy.
$terms = wp_get_post_terms($post_id, 'property-city');
foreach($terms as $term) {
// Traverse up the term hierarchy until a top-level term (parent = 0) is reached.
while($term->parent != 0 && !has_term($term->parent, 'property-city', $post)) {
// Assign the parent term to the post.
wp_set_post_terms($post_id, array($term->parent), 'property-city', true);
// Move to the next parent term.
$term = get_term($term->parent, 'property-city');
}
}
}
The idea is that we have a property-city taxonomy with parent and child terms (state and city). In Bricks, we logically want to display it this way, for example: California, Los Angeles.
In Bricks, we have to do this in my case I don’t want link.
Of course, you should carefully arrange your states and cities. This applies to any other form of taxonomies with parent > child relationships.
The reason for this post is to share this little trick. While creating another site, I forgot it, so I looked for my snippet that I easily forget.
Maybe (who knows) it will inspire developers or people to improve this mechanism and include it in a phase 2.0 of Bricks.
this way you avoid creating multiple taxonomies and headaches,
if you want you can show us another way or play with this code to create more sophisticated things in my case a level 1 child > parent is enough
I think I provided enough clear information to initiate a deep reflection