Create a custom taxonomy that will be used to create and filter markers in a Google Map

I'm trying to create a Wordpress shortcode that will output a full-width, filterable Google Map with a set of markers that will be loaded on first load (and shown or hidden depending on the filters chosen).

The thing is, I've never done / used a custom taxonomy before, and I thought that it would be a good idea to create one, I was thinking about "a class" called Marker with the attributes "latitude, longitude, title and description", but I am not sure on how to proceed in order to achieve this.

Can someone please help me on how to create this taxonomy, and then load all of it's items in Wordpress programatically? Thanks in advance.

Topic google-maps map shortcode functions custom-taxonomy Wordpress

Category Web


Here's a fully working example to get you started.

Register the "marker" taxonomy

register_taxonomy( 'marker',
    'post',
    array(
        'hierarchical'          => false,
        'update_count_callback' => '_update_post_term_count',
        'label'                 => __( 'Markers', 'textdomain' ),
        'labels' => array(
                'name'              => __( 'Markers', 'textdomain' ),
                'singular_name'     => __( 'Marker', 'textdomain' ),
                'menu_name'         => _x( 'Markers', 'Admin menu name', 'textdomain' ),
                'search_items'      => __( 'Search Markers', 'textdomain' ),
                'all_items'         => __( 'All Markers', 'textdomain' ),
                'parent_item'       => __( 'Parent Marker', 'textdomain' ),
                'parent_item_colon' => __( 'Parent Marker:', 'textdomain' ),
                'edit_item'         => __( 'Edit Marker', 'textdomain' ),
                'update_item'       => __( 'Update Marker', 'textdomain' ),
                'add_new_item'      => __( 'Add New Marker', 'textdomain' ),
                'new_item_name'     => __( 'New Marker Name', 'textdomain' )
            ),
        'rewrite'               => false,
    )
);

Now we need to add the hooks to add/save the fields we need for this taxonomy

add_action( 'marker_add_form_fields',   'marker_add_fields', 10, 2 );
add_action( 'marker_edit_form_fields',  'marker_edit_fields', 10, 2 );
add_action( 'created_marker',           'save_marker_meta', 10, 2 );
add_action( 'edited_marker',            'save_marker_meta', 10, 2);

This function adds the fields to add new taxonomy screen

function marker_add_fields() {
    ?>
    <div class="form-field">
        <label for="latitude"><?php _e( 'Latitude' ); ?></label>
        <input type="text" name="latitude" id="latitude" value="">
    </div>

    <div class="form-field">
        <label for="longitude"><?php _e( 'Longitude' ); ?></label>
        <input type="text" name="longitude" id="longitude" value="">
    </div>
    <?php
}

This function add the fields to the edit screen of taxonomy

function marker_edit_fields( $term, $taxonomy ) {
    $latitude   = get_term_meta( $term->term_id, 'latitude', true );
    $longitude  = get_term_meta( $term->term_id, 'longitude', true );
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="latitude"><?php _e( 'Latitude' ); ?></label></th>
        <td>
            <input type="text" name="latitude" id="latitude" value="<?php echo $latitude; ?>">
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="longitude"><?php _e( 'Longitude' ); ?></label></th>
        <td>
            <input type="text" name="longitude" id="longitude" value="<?php echo $longitude; ?>">
        </td>
    </tr>
    <?php
}

Finally, this function saves the custom field values for our new taxonomy

function save_marker_meta( $term_id, $tt_id ){
    if( isset( $_POST['latitude'] ) && '' !== $_POST['latitude'] ){
        update_term_meta( $term_id, 'latitude', $_POST['latitude'] );
    }
    if( isset( $_POST['longitude'] ) && '' !== $_POST['longitude'] ){
        update_term_meta( $term_id, 'longitude', $_POST['longitude'] );
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.