Changed API > Need Mashup based on custom fields

Home Forums MapPress Support Changed API > Need Mashup based on custom fields

Tagged: , ,

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #19591
    jcdesign
    Participant

    Hi there,

    I started with version 2.75.1, and rolled my own custom map, essentially a mashup, based on a “businesses” custom post type. The map allowed me to filter based on a custom taxonomy, first as the page was loaded (e.g., “Dining”), then by clicking on various business types (e.g., “Mexican”).

    I was probably reinventing the wheel, but in any case, it relied on resetting POIs using mapp0.setPois(), which no longer exists in your newer update.

    I’m happy to use the mashup block instead, but I can’t get this to even load the first map. If I could do that, I believe I could alter the filtering via AJAX and deal with the rest.

    I have the query like this:

    {
        "post_type": [
            "is"
         ],
        "tax_query": {
            "ldba_type": {
                "taxonomy": "ldba_type",
                "terms": [
                    243
                ],
                "operator": "IN",
                "include_children": true
            }
        }
    }

    The lat/lng for each business is stored as a custom field, ‘latlng’, with value like this:

    39.97684,-105.13226

    I tried adding this custom field to the mashup, but similarly got no results. What am I missing here?

    Thanks!

    Of note: I’m working with a Full Site Editing theme. I disabled all plugins except for the code generating the appropriate custom post types/taxonomies, and changed to Twenty Twenty Two theme, but still no results.

    Also possibly notable: every time I try to view the shortcode for the Mashup, the block gets corrupted, and I see “This block has encountered an error and cannot be previewed.”

    #19592
    jcdesign
    Participant

    Also tried a simpler query, no results

    {
        "post_type": [
            "is"
         ]
    }
    #19593
    Chris
    Keymaster

    Hi,

    That JSON doesn’t look valid.  MapPress uses the WordPress wp_query module, so those are the parameters that are supported.

    Instead of entering raw JSON, try just using the post type using the checkboxes for the mashup block.  That should give something like this (for a ‘location’ custom post type):

    {
    "post_type": [
    "location"
    ]
    }

    It’s not possible to generate a mashup directly from custom fields.  You have to first create a map for each post, based on the custom fields.  Once the individual post maps exist, you can create a mashup combining all of the maps together.

    https://mappresspro.com/mappress-documentation/#toc-generating-maps-from-custom-fields

    I was able to reproduce the crash when displaying the shortcode, which occurs because the JSON is invalid, and it’ll be fixed ASAP in version 2.83.8.  Thanks for reporting it!

    #19594
    jcdesign
    Participant

    I really don’t want to need a map for each post! Will keep digging. The JSON was auto-generated. I wasn’t trying to enter the JSON, just thought that could help point in the right direction. Thanks!

    #19596
    Chris
    Keymaster

    Ok.

    I’ve removed the buggy shortcode tab for the mashup block.  It worked for earlier versions, but the block now supports queries that are too complex to use in a shortcode.

    I don’t know how the invalid JSON was created, but if you’d like to provide a step-by-step I’ll try to reproduce the problem.

    I know what you mean about not wanting a map for each post, but those maps don’t need to be displayed – they just need to exist and be linked to the post in order to generate a mashup.

    The maps are now React components, so JavaScript methods like setPois are no longer present.  But it’s still possible to set the POIs from PHP.  Filter ‘mappress_post_query’ can be used to modify the results for a mashup map.  The filter is applied when a mashup query is run, see plugin file ‘\pro\mappress_query.php’.  The filter is called with a map object and the original query, and you can use it to set the POIs for the map.

    If you need more info, please let me know.

    #19623
    jcdesign
    Participant

    Hi Chris,

    I’m circling back to this, still somewhat stuck. (BTW, my post type is “is”, that may have confused you earlier!)

    I see that the mappress_post_query filter is run via AJAX. That seems like I could then manipulate on the fly from the front end — is that correct? And if so, is there any chance you can supply a short code example showing how to interact with the filter?

    This is a very simplified version of what I have so far:

    class MyMapClass {

    public function __construct() {
    add_filter( ‘mappress_post_query’, array( $this, ‘filter_map’ ), 10, 2 );
    }

    /**
    * @param Mappress_Map $map
    * @param array $args
    *
    * @return Mappress_Map
    */
    public function filter_map( $map, $args ) {

    // Do stuff here
    $map->pois = [ ‘foo’ ]; // TESTING filter  to see if I can break things – no changes

    return $map;
    }

    }

    new MyMapClass();

    I’m not seeing any effect on the maps I’ve generated via PHP. Any guidance would be appreciated — thank you.

    #19625
    Chris
    Keymaster

    Hi,

    I think it would be easier to just let MapPress generate the necessary maps, and use a mashup block to display them.  You could then use the standard filtering/query by post type & taxonomies.

    But if you’d like to use the filter, the example code below worked for me (I added it to the theme’s ‘functions.php’).

    function myfilter($map, $args) {
    $poi = new Mappress_Poi(array('title' => 'mypoi', 'point' => ['lat' => 22, 'lng' => 23]));
    $map->pois = [$poi];
    return $map;
    }
    add_filter('mappress_post_query', 'myfilter', 10, 2);

    You can also use filter ‘mappress_query_pois’ which just replaces the POIs.  There’s an example of using that filter in ‘mappress_frontend.php’, which fetches POIs from data stored in the Advanced Custom Fields plugin.

    #19626
    jcdesign
    Participant

    Thanks, I’m exploring both. I’ve been resistant to adding maps to posts, because this is a membership site where businesses submit info, including their address. I don’t want to add to my task list for each time a new business signs up. Perhaps I can auto-generate a Mappress Map as the post gets created, might explore that.

    That said, I tried a mashup, and I still don’t see how it helps me. I don’t think I’ve clearly explained what I’m trying to do — it might help if you can see it in action (with the 2.75.1 version of Mappress Pro).

    Currently on my development site, clicking on a sub-category limits both the listing and the points displayed on the map.

    The whole point (for me) is to be able to filter businesses on the fly and display them on the map. Having a method to interact via JavaScript is really the key to this. If I can do a reload via AJAX and filter my data, fine. But it really needs to update the map in real time, without a reload. For now, I think I need the public API in 2.75.1 more than other recent updates (although you fixed a bug I noticed w/ polygons).

    If I’m missing something, would love to hear it!

    Thanks,
    Jeremy

     

     

    #19627
    Chris
    Keymaster

    Thanks, the link was helpful.

    The plugin will auto-generate maps for you, so there’s no extra step.  But you’d need to use the mashup, along with its ‘filter’ button and POI list for the results.  You can see the map on my home page for an example.

    If you need to do something custom: the map is now a React component, so the imperative interfaces (e.g. setPois) are gone.  If you’re interested in loading React on the page, I can explain how to display the map POIs using ReactDom.render() : https://reactjs.org/docs/react-dom.html#render

    If the above solutions don’t work, then you may want to remain on 2.75 for now, and I’ll look into adding an external interface (web component or imperative methods) for a future release.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.