• Resolved searay

    (@searay)


    On my site there is a google map with markers for all the properties. I need to place that map with markers on one of my pages, which contains an agent and all his/her properties. Both agents and properties are custom posts. Markers are made of properties addresses.

    So far I was able to put the map, but it shows the markers for all the properties available. I don’t know how to target only these few properties that are assigned for particular agent.

    The code that checks if google map is needed looks like this: https://pastebin.com/hQXDengG

    I modified it by adding
    || is_singular( 'agent' )
    on line 26.

    When I tried to target specific agent with “is_single(‘agent_name’); it is doing the same thing, showing all the properties. The question is how to modify this code to target every agent separately. I guess I need some kind of if statement.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator bcworkz

    (@bcworkz)

    The markers on the map need to be managed with the map related javascript. The best way to pass PHP data to javascript is to use wp_enqueue_script() to cause your script to be loaded with the page, then use wp_localize_script() to pass the data. You can construct the data by using WP_Query to get the appropriate posts, then loop through the results to construct the JS data array.

    About the only thing is_singular() is good for is to only enqueue your JS when necessary instead of for every request.

    Thread Starter searay

    (@searay)

    Ok, I partly solve my problem, the map is working good now and showing only the posts that I wanted to.
    All I needed to add was an array within an array.

    Now, when the markers are clicked they show property thumbnail with the link to this property page. What I want, is to add another link below with that property’s Agent name that links to the agent’s page. Can you guide me through the WP_Query to get appropriate posts? Or maybe there is a better way?

    I know I need to edit javascript for that, I more less know where and how, what I do not know is how to pass the data with relevant agent’s ID and link it to the agent’s page. With only one agent it would very easy, but when I have 180 agents…

    Here is the code I’m referring too link

    Moderator bcworkz

    (@bcworkz)

    I’ll do what I can to help. How do the agent posts relate to the property posts? Is there an agent post ID in the property’s meta data by chance?

    Thread Starter searay

    (@searay)

    I don’t think there is agent post ID in the property’s meta, but to be honest I don’t know. How can I check that?

    This is the code of the page that shows single property and an agent assigned to it in the bottom.

    And this is the code for that bottom part. It works as a separate template and it’s called by get_template_part(‘property-details/property-agent’); function.

    I tried to find any relation between agent and property but with no luck. I hope you can help me with this.

    Moderator bcworkz

    (@bcworkz)

    Thanks for posting that code, but I’m not seeing anything helpful there. What I would do in this situation is look directly at the DB to try and identify how agents relate to a property. This is done through phpMyAdmin, which can usually be accessed through your host’s control panel. Usually near the bottom under Databases. This is very powerful software, if you’re not careful, you can really mess up the DB with it. Don’t try to do anything that changes data, just look.

    If you can identify one property ID and it’s associated agent from the website, it will help you when looking at the DB because you’ll then know what you’re looking for. In phpMyAdmin, browse to the wp_postmeta table, then click the search tab. Enter the property ID in the post_id field and click Go. The results will be all postmeta for the property. Hopefully you’ll see something in meta_value that indicates what agent is associated with the property. It may just be an ID, so it would help if you knew what the agent’s ID is beforehand. Make note of the meta_key of anything that relates to agents.

    If that doesn’t turn up anything, reverse the process. Search by agent ID and try to find something about the agent’s associated properties. Good luck!

    Thread Starter searay

    (@searay)

    I did what you asked and found at least two agent IDs associations. snapshot here I see two associations there. Well, third one REAL_HOMES_property_contractor is a metabox in admin created by me for the purpose of displaying agent’s name on the property preview.
    Anyways.. the REAL_HOMES_agents is the one I need I think. The second one, REAL_HOMES_agent_display_option is used for the agents dropdown. So the ID would be 1062. What’s next?

    Moderator bcworkz

    (@bcworkz)

    Awesome! Well done.

    What’s next depends on what 1062 leads to. If it’s a user ID, then the agent’s post still needs to be found. If it’s an arbitrary ID outside of the WP system, the agent’s post still needs to be found. It’s hopefully the agent’s post ID, in which case you can get the link with get_permalink( 1062 ). To get the agent’s name, it depends on where it is stored. Post meta perhaps? More poking around in phpMyAdmin should tell you what you need to know.

    To get that agent ID 1062 value for any given property, if you have the property post ID, you use get_post_meta( $property_id, 'REAL_HOMES_agents', true)

    Thread Starter searay

    (@searay)

    Great, thanks! I was able to make a link that works in php, but having troubles to pass that info with agent ID to the javascript.

    All the info is stored in the $properties_data variable, how can I pass the Agent ID to it? I tried with the code below and no luck, cause I don’t know how to define another url in javascript.

    That’s the part of the code pasted above with my modification.

    /* Agent ID */
    
    				 $post_data = $post->ID;
    
             $agent_id = get_post_meta($post_data, 'REAL_HOMES_agents',true); 
    
             $current_prop_array['builder'] = $agent_id[0];

    and this is part of the javascript from the same code above which I can’t make it to work to show the link with Agent ID on the infoview window.

    var innerHTML = "";
                    if ( properties[i].thumb ) {
                        innerHTML += '<a class="thumb-link" href="' + properties[i].url + '">' +
                                    '<img class="prop-thumb" src="' + properties[i].thumb + '" alt="' + properties[i].title + '"/>' +
                                    '</a>';
                    }
    
                    innerHTML += '<h5 class="prop-title"><a class="title-link" href="' + properties[i].url + '">' + properties[i].title + '</a></h5>';
    
                    if ( properties[i].price ) {
                        innerHTML += '<p><span class="price">' + properties[i].price + '</span></p>';
                    }
    
                    innerHTML += '<div class="arrow-down"></div>';
    
                    boxText.innerHTML = innerHTML;

    Have a question, can the “url” in the code below consist more than one link? Or do I need to define another variable similar to “properties” and assign new “url” to it?

    innerHTML += '<h5 class="prop-title"><a class="title-link" href="' + properties[i].url + '">' + properties[i].title + '</a></h5>';

    Just to make it easier Im pasting the code from above again here.

    Thread Starter searay

    (@searay)

    UPDATE:

    I managed to pass the agent id with a link into the javascript using the code structure that was already in place. Which means I replaced the link that was linking to the property with the link that is linking to agent’s page. Success!
    This is the code I used:

    $post_data = $post->ID;
            $agent_id = get_post_meta($post_data, 'REAL_HOMES_agents',true);
            if(!empty($agent_id)) {
            $current_prop_array['url'] = get_permalink($agent_id);
            }

    The only question now is how can I pass both links in one variable $current_prop_array[‘url’]? I want to keep the property link at least for an image thumbnail. If there is no option for that I would need to probably define another variable in javascript or maybe there are other easier ways? Im guessing.. as always.
    Thanks for your great help @bcworkz, again I learned a lot here. Still hoping for more.

    Thread Starter searay

    (@searay)

    I solved the problem myself. Just used the same variable in php just with different naming in the square brackets and after updating javascript that works fine! No major changes to the javascript code needed which is great news also. Thanks @bcworkz for your help!

    Moderator bcworkz

    (@bcworkz)

    You are currently using json_encode() to serialize the current property data so JS can make use of it. This works great and you can build upon that. It may help to output the current $properties_data so you can see the data structure. You can add this at the end of your code to see the structure:

    <pre>
    <?php print_r( $properties_data ); ?>
    </pre>

    Without that output it’s difficult to be sure of the structure but it appears to be an indexed array of associative arrays where each associative array represents a marker. You can simply add new keys to that structure to accommodate more data.

    For example, the first marker’s property URL (before you changed things) was in $properties_data[0]['url'] which in JS was used as properties[0].url. The 0 of course becomes 1 for the second marker, 2 for the third, etc.

    You can assign the agent’s URL value to $properties_data[0]['agent_url'] and the agent’s name to $properties_data[0]['agent_name']. Then in JS you can use those values with properties[0].agent_url and properties[0].agent_name. You can use whatever key name makes sense as long as it’s coordinated on the JS side.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Target specific group of posts’ is closed to new replies.