Ross
Ross

Reputation: 534

How to Filter Output for a Custom Field Key in Elementor Dynamic Tags

I'm using Elementor Pro and WordPress, and I'm trying to filter a custom field's output specifically in Elementor > Dynamic Tags > Post Custom Field. My custom field key is _department, and I'm storing values as slugs (e.g., residential-sales and residential-lettings). I want to map these values to display-friendly labels like “Sales” and “Lettings” when the _department key is used in Elementor, without affecting other areas site-wide.

Code So Far:

I've written the following code to attempt this:

function custom_elementor_department_field_output( $value, $meta_key, $post_id ) {
    // Check if the meta key is '_department'
    if ( '_department' === $meta_key ) {
        // Map stored values to user-friendly names
        switch ( $value ) {
            case 'residential-sales':
                return 'Sales';
            case 'residential-lettings':
                return 'Lettings';
            default:
                return $value; // Return the original value if no match
        }
    }

    return $value; // Return the original value for other meta keys
}

// Attempt to add filter
add_filter( '...', 'custom_elementor_department_field_output', 10, 3 );

Problem:

I found add_filter( 'elementor_pro/posts/posts_custom_fields/get_meta' ... ) somewhere but i don't believe it even exists, and it obvi doesn't work. It may be the right approach, if I could get thi working as expected in Elementor's dynamic tags. Specifically, I need this to only affect the _department key in Elementor dynamic tags and not elsewhere.

Questions:

  1. Is this the correct filter to use for modifying dynamic tag outputs in Elementor?
  2. If not, could you provide guidance on the correct approach to filter a custom field’s output specifically in Elementor's dynamic tags?

Thanks, Any help or suggestions would be greatly appreciated!

Upvotes: 1

Views: 111

Answers (0)

Related Questions