Skip Navigation

[Resolved] Need to create a to and from field

This support ticket is created 5 years, 6 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 3 voices.

Last updated by matthewL-6 5 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1256907

Tell us what you are trying to do?

I have been asked to rebuild an airport transfers website.

It is fairly simple all fixed prices for routes. However they want the user to be able to select the "from" and then "to" options and then it must display the options available.

I have tried adding a post type destinations and creating a post type routes.

I have tried one, many to many relationship, this does not work.

I have tried two, many to many relationships one called "to" and one called "from" but both linking to destinations.

None of these work as you can only have one relationship filter.

Ideally I would have a parent post type destinations to display all the routes available to that destination.

I wonder is it possible to populate two drop downs every time someone adds a new destination but not actually link to them in anyway, more store them as a field value, which would enable the admin to add a new destination and then add a new route and select the to and from fields which only show the destinations as a drop down, which then would allow the front end user to filter by to and from locations.

I have tested this by manually adding the values and it works well.

I can write and understand basic php and jQuery.

#1257777

Hello,

The post type relationships between posts of same post type is in our development plan, so currently, it needs at least two post types to setup the post type relationships, for example:
1) Setup two post types:
- destinations
- routes

2) Setup two post type relationships:
- One-to-many relationship "From" between post type "destinations" and "routes"
- One-to-one relationship "To" between post type "destinations" and "routes"

3) Create/edit each "routes" post, relate it with other "destinations" posts using above "From" and "To" relationships.

4) In a single "destinations" post, you can setup a post view:
- Query "routes" posts

- filter by relationship "From"
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/#displaying-many-related-items
Front-end search form filter:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/how-to-filter-posts-by-their-ancestors/

- Display related "destinations" post's information by relationship "To"
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/#displaying-one-related-item-parent

#1262613

Hi Lou,

Sorry for the late reply, I tried this and it did not work. I tried a few other post relationships and realised the issue is that I cannot create a ancestioral relationship with the same post type, event if the realtionshios are different.

It just says no ancestors are deifined.

Any other suggestions.

Thanks.

#1262641
#1263663

The filter hook "wpt_field_options" is still possible within current version of Toolset plugins, it is available in custom radio field or select field.

But it requires custom PHP codes, you can try it in your website, and feedback if there is problem in it.

#1265729

Thanks lou, that worked on the backend but not the front end in an archive as a filter.

Can it work on the front end as a filter?

Here's my code:

add_filter( 'wpt_field_options', 'func_to_dynamically_populate_select', 10, 3);
function func_to_dynamically_populate_select( $options, $title, $type ){
    switch( $title ){
        case 'From Location':
            $options = array();
			$args = array(
                'post_type'        => 'destination',
                'post_status'      => 'publish');
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

add_filter( 'wpt_field_options', 'func_to_dynamically_populate_select_2', 10, 3);
function func_to_dynamically_populate_select_2( $options, $title, $type ){
    switch( $title ){
        case 'To Location':
            $options = array();
			$args = array(
                'post_type'        => 'destination',
                'post_status'      => 'publish');
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

Alternatively are any of the following possible. I am not asking you to tell me how.

Create a duplicate post every time a post is added.

I'm thinking two post types Destination and from location but I do not want the client to have to add the post twice.

or

Use javascript to hook into the the types relationship api but I do not see the options I need here.

Or

Most complicated.... Create a custom rest api add necessary fields use javascript to display them and create

What wold you do?

I can make php work, and Js but i do not know all the possibilities yet as I have onlydone a Jquery Course. So anypointers would help.

thanks

#1266673

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Luo is on vacation this week. This is Minesh here and I'll take care of this ticket, hope this is OK.

As I understand, you want to implement the filter wpt_field_options for front-end filters (using custom search view) - correct ? If yes, this filter will not have any effect on front-end filters.

If you can tell me what you want to achieve, once I understand your requirement I will be able to guide you in the right direction.

#1270589

Hi Minesh,

sorry for the delay.

As this filter may be obsolete at anytime I realised this probably wasn't the best option.

So I found a much simpler solution. I have created a taxonomy called "to destination" and written a really simple function that adds a new taxonomy every time I add a post under the post type destinations.

function add_destination_category_automatically($post_ID) {
global $wpdb;
if(!has_term('','to-destination',$post_ID)){
    $cat = get_the_title($post_ID);
    wp_set_object_terms($post_ID, $cat, 'to-destination');
}
}
add_action('publish_destination', 'add_destination_category_automatically');

I have also removed all of my other post types and opted for woocommerce so that I can easily add guest check out and manage payments.

It seems to work a treat, now all I need to do is create a wildcard redirect for all taxonomies with the same name as the post types.

Can you see any issues with this setup.

A demo site should be finsihed some time today.

Thank you

#1271701

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It seems to work a treat, now all I need to do is create a wildcard redirect for all taxonomies with the same name as the post types.
=> I do not understand this line, can you please explain a bit more - the workflow?

#1284183

My issue is resolved now. Thank you!