Magic function for wordpress/bricks

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.

Capture d'écran 2024-09-28 103645

In Bricks, we have to do this in my case I don’t want link.

Capture d'écran 2024-09-28 103739

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

4 Likes

Super helpful. nice little surgical function to assign parent terms. Like it. =)

1 Like

Thank you so much for this! I have site I’m working on with intense hierarchical product taxonomies and this is… (sheds tear) so helpful!

1 Like

Since someone liked the first magic snippet :disguised_face:,

let’s continue with the second one, which is also very useful for WooCommerce also.

<?php

/**
 * Restricts product attachment selection to the current post in the media library
 *
 * This function modifies the attachment query arguments to only show attachments
 * associated with the current product being edited.
 *
 * @param  array $query The current attachment query arguments.
 * @return array       The modified attachment query arguments.
 */
function restrict_product_attachment_to_current_post($query) {

  // 1. Validate user permissions (unchanged)
  if (!current_user_can('upload_files')) {
    wp_send_json_error();
  }

  // 2. Ensure a valid post ID is provided (unchanged)
  if (!isset($_REQUEST['post_id'])) {
    wp_send_json_error();
  }

  // 3. Retrieve the post object (unchanged)
  $post = get_post((int)$_REQUEST['post_id']);
  if (!$post instanceof \WP_Post) {
    return $query;
  }

  // 4. Restrict to WooCommerce product posts and their variations
  if ($post->post_type !== 'product' && $post->post_type !== 'product_variation') {
    return $query;
  }

  // 5. Filter by post parent (product) or post ID (variation)
  $query['post_parent'] = ($post->post_type === 'product') ? $post->ID : $post->post_parent;

  return $query;
}

add_filter('ajax_query_attachments_args', 'restrict_product_attachment_to_current_post');

Applying this snippet allows you to stop the madness of displaying all the images/products in the media library. In most projects like real estate, WooCommerce, cars, etc., this makes no sense.

For example, let’s imagine I work on a Xiaomi phone website. Everything goes well until I upload the product images, and then the drama begins.

All the phones are displayed, and all the colors look almost the same. I challenge you to tell the difference between the Redmi Note 13 and the 13 Pro and 13 Pro+. It’s madness!

We don’t have time to name a thousand images, sort them, or manage them. We just want to upload, select the images, and be done with it. This is where the snippet comes in.

  • Apply the snippet You can, of course, customize which Custom Post Types (CPT) it should work on.
    preview

Now, you should only see the media uploaded to the current post and not all posts or all images.

And of course in the same spirit as the first snippet this could very well find a place in bricks > settings
in the form of " limit uploaded media to their post : checkbox your cpt