Skip Navigation

[Resolved] Automate field based on category

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 1 year, 9 months ago.

Assisted by: Nigel.

Author
Posts
#2654107

I have a map linked to an toolset image field where it uses a coloured pin to display on the map depending on which category they belong to. Red pin for Category 1 and Blue for Category 2. I would like to automate this process if possible.

The link to the actual map page is hidden link

I have a category set up named Membership Types with taxonomies set up as Category 1 and Category 2.

And an toolset image field set up named map-pin which I then use an image named pin-cat1.png image for Category 1 and pin-cat2.png image for Category 2

What I would like to happen is that when they belong to the Category 1 the toolset map-pin field automatically chooses the image pin-cat1.png and then if they belong to Category 2 the toolset map-pin field automatically chooses the image pin-cat2.png

Is this possible to do with some coding.

#2654193

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Karen

Can I check how you've done this so far? Looking at the page I would have expected this to be created using a View (and my question would be whether you were using the legacy Views editor or the block editor), but checking the source markup it doesn't look like you've used a Toolset View or a Toolset Map at all, and are using some other solution altogether.

So I'm not clear what you need, as I don't know how this is set up, how you are adding markers to a map, and how you are currently setting some of those to red, and others to blue.

#2654209

I have used a separate search and filter plugin with dynamic.ooo google map widget rather than using view as it did not allow for all the functionality I required. The google map widgets allows me to choose an image from a acf or toolset field.

But what I really want is for my toolset field I have set up called map-pin to choose the correct image based on the category type that was chosen eg Category 1 or Category 2. The plugin that I then use for the map will then have the correct image already chosen to use for the map.

So if I can get toolset to populate the following
Membership type Category 1 is selected - this then populate the map-pin field with the image pin-cat1.png
Membership type Category 2 is selected - this then populate the map-pin field with the image pin-cat2.png

#2654385

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, understood.

To help with this, can you describe when/how the data is entered?

If you are adding posts (that will appear on the map) and these posts should have a custom field set to the marker image—depending on the category—then it seems like the time the post is published is the time to intervene and set the field value as required.

If you are using a Toolset form to publish posts from the front end that's easy enough, using the Forms API hooks.

If using some other method you may need something else.

Can you clarify?

#2654441

I do have a form set up for members who purchase membership online, see link below.

hidden link

However I also add members in by adding a new post as well. Can it populate the map-pin field when the post is saved then it would cover both options.

#2654469

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

In that case you would need to use the WordPress hook save_post, see https://developer.wordpress.org/reference/hooks/save_post/

I've drafted some sample code that you can modify for your needs. It starts by defining a number of variables that would be specific to your site, and if you set those right I think it should work, but naturally I'm not in a position to test.

Writing custom code is outside the scope of support, and while I hope it meets your needs, if not you may need to contact a developer to produce a working solution.

function ts_set_marker_icon( $post_id, $post, $update ){

    $post_type = 'profile'; // post type this code should apply to
    $field_key = 'wpcf-marker-icon'; // custom field to store icon url, note Toolset fields include a 'wpcf-' prefix
    $marker1 = '<em><u>hidden link</u></em>'; // URL of marker for category 1
    $marker2 = '<em><u>hidden link</u></em>'; // URL of marker for category 2
    $taxonomy = 'membership-type'; // slug of taxonomy that stores the category
    $term1 = 'one'; // slug of the term for category 1
    $term2 = 'two'; // slug of the term for category 2

    if ( $post->post_type != $post_type )
        return;

    // What is the category value of this post?
    $categories = get_the_terms( $post_id, $taxonomy );

    if ( $categories ) {
        $category = $categories[0]; // it should only have one category
        if ( $category->slug == $term1 ) {
            update_post_meta( $post_id, $field_key, $marker1 );
        } elseif ( $category->slug == $term2 ) {
            update_post_meta( $post_id, $field_key, $marker2 );
        }
    }

}
add_action( 'save_post', 'ts_set_marker_icon', 10, 3 );
#2657285

Perfect, many thanks