Skip Navigation

[Resolved] Distance filter doesnt’t work

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two post types in a one-to-many relationship. I would like to create a custom search View of the child post type, filtering by distance based on an address field in the parent post type, and include other filters based on custom fields in the child post type.

Solution: A View of post type A cannot filter by fields in post type B, so you would have to copy the parent address field into the child posts automatically using custom code, then filter by the address field in the child post type.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1936053

Hello, this is David

I have a problem with a distance filter.

The map is here hidden link

That map show MENUS of restaurants. The markers are using the address of restaurant (marker_field='wpcf-adreca-del-negoci' item='@negoci-menu.parent')

The distance fiter is compairing the location with adreca-del-negoci (of RESTAURANT but I'm using menus not restaurants). So... when I filter by distance the filter is not working. HOw can I filter by adreca-del-negoci of parent post?

Thanks in advanced!

#1937083

Hello David,
It sounds like you want to filter a View of Menus based on an address field in a related post type Restaurants. In general it is not possible to filter a View of post type A based on fields in a related post type B. If you want to filter a View by distance, the address field must be applied to the post type displayed in the View results. Since there is only one filter in this View, you might be able to achieve something similar with a View of Restaurants instead of a View of Menus. If you plan to allow filtering based on other fields, it becomes more complex depending on whether those fields are in the Restaurant post or the Menu post.

If you want to display Menus in a View of Restaurant posts, you may be able to use a nested View structure. Unfortunately the Block Editor does not support creating nested Views, so the legacy Views editor is required here. I can offer more guidance if you can explain a bit more about what you want to achieve. Is it possible for one Restaurant to have more than one related Menu? In other words, what kind of post relationship is set up between Restaurants and Menus - is this connection made by a post reference field, a one-to-one relationship, a one-to-many relationship, or a many-to-many relationship?

#1938247

Hello Christian.

This is an example: I want to get all menus (in a map) of all restaurants in 2 km around me.

One restaurant can have a many menus.

The menus are shown in the map with the address of parent restaurant address.

With nested view... I wourld filter by custom fields of menus? I think... NOT. Or Yes?

Then... the only way to filter menus by distance is that menus have their own address? Is there any way to save adress of restaurant in a custom field of menu when I create or edit a menu with a form?

Or there is other way?

#1938621

With nested view... I wourld filter by custom fields of menus? I think... NOT. Or Yes?
No, unfortunately that would not be possible. You could only filter by fields of Restaurants in this case, since the parent View is a view of Restaurants.

Then... the only way to filter menus by distance is that menus have their own address?
Yes, that is correct. If you want to filter menus by distance AND by other custom fields in the menu post type, you must include an address in every Menu post.

Is there any way to save adress of restaurant in a custom field of menu when I create or edit a menu with a form?
You could create an address field on the Menu post type, and add the address manually in the Form for each Menu post. Or, you could use custom code to automatically copy the address from the parent post into an address field in the menu post. We have an API available cred_submit_complete that can be used to automate a custom field value. We have an API available toolset_get_related_post that can be used to get information from a parent post. Here's an example using those APIs together:

// https://toolset.com/forums/topic/distance-filter-doesntt-work/
// automatically copy parent address field into child address field
add_action('cred_submit_complete', 'tssupp_copy_parent_address',10,2);
function tssupp_copy_parent_address($post_id, $form_data)
{
  $forms = array( 123, 456 );
  $relationship_slug = 'book-chapter';
  $parent_address_slug = 'parent-address';
  $child_address_slug = 'child-address';
  // you should not edit below this line

  if ( in_array( $form_data['id'], $forms ) )
  {
    $parent_id = toolset_get_related_post( $post_id, $relationship_slug, 'parent' );
    if ( $parent_id )
    {
      $parent_address = get_post_meta( $parent_id, 'wpcf-' . $parent_address_slug, true);
      if ( $parent_address )
      {
        update_post_meta( $post_id, 'wpcf-' . $child_address_slug);
      }
    }
  }
}

You would replace 123, 456 with a comma-separated list of the numeric IDs of any Forms that create child posts. You can find those IDs in Toolset > Post Forms. You would replace book-chapter with the slug of the relationship between restaurants and menus. You can find that slug in Toolset > Relationships. You would replace parent-address with the slug of the address field in the Restaurants post type. You can find that slug in Toolset > Custom Fields > Post fields tab. You would replace child-address with the slug of the address field in the Menus post type.

You should add this code in a child theme's functions.php file, or add it to a new custom code snippet in Toolset > Settings > Custom Code. Set the snippet to "run everywhere" and be sure to Activate the snippet. When you submit the Form to create a new child post, the address field from the parent post will be automatically copied into the Menu post.

Documentation for these APIs and the WP APIs used in the code is available here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/update_post_meta/

#1940951

Thanks Christian. I'll test it ASAP

#1941007

Hello again Christian. Following your instructions, when the snippet is activated when I modify a menu the web crash with a critical error. How can I see what is happening?

add_action('cred_submit_complete', 'tssupp_copy_parent_address',10,2);
function tssupp_copy_parent_address($post_id, $form_data)
{
  $forms = array( 257, 272 );
  $relationship_slug = 'negoci-menu';
  $parent_address_slug = 'adreca-del-negoci';
  $child_address_slug = 'adreca-del-menu';
  // you should not edit below this line
 
  if ( in_array( $form_data['id'], $forms ) )
  {
    $parent_id = toolset_get_related_post( $post_id, $relationship_slug, 'parent' );
    if ( $parent_id )
    {
      $parent_address = get_post_meta( $parent_id, 'wpcf-' . $parent_address_slug, true);
      if ( $parent_address )
      {
        update_post_meta( $post_id, 'wpcf-' . $child_address_slug);
      }
    }
  }
}

Inside the snippet is

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_action('cred_submit_complete', 'tssupp_copy_parent_address',10,2);
function tssupp_copy_parent_address($post_id, $form_data)
{
  $forms = array( 257, 272 );
  $relationship_slug = 'negoci-menu';
  $parent_address_slug = 'adreca-del-negoci';
  $child_address_slug = 'adreca-del-menu';
  // you should not edit below this line
 
  if ( in_array( $form_data['id'], $forms ) )
  {
    $parent_id = toolset_get_related_post( $post_id, $relationship_slug, 'parent' );
    if ( $parent_id )
    {
      $parent_address = get_post_meta( $parent_id, 'wpcf-' . $parent_address_slug, true);
      if ( $parent_address )
      {
        update_post_meta( $post_id, 'wpcf-' . $child_address_slug);
      }
    }
  }
}
#1941021

I apologize for this error, it seems I did not copy + paste the code correctly. Please change this line of code (near the end of the snippet):

update_post_meta( $post_id, 'wpcf-' . $child_address_slug);

The code should be:

update_post_meta( $post_id, 'wpcf-' . $child_address_slug, $parent_address );

Resave the snippet and test once again. If that does not resolve the problem, please provide login credentials in the private reply fields here so I can take a closer look.

#1941125

Thanks a lot Christian for your time and help.
Your support rocks!
My issue is resolved now. Thank you!