Skip Navigation

[Resolved] Parametric search – filtering by parent taxonomy + custom date field

This support ticket is created 4 years, 4 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 13 replies, has 2 voices.

Last updated by Waqar 4 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1715025

I have been having some problems with a complex parametric search I am setting up.

The search is of a child post type, with two different classes of parent.

I have set up the view to display a table including fields and taxonomies from both the child post type and both parents and wish to set up filters to appear above the table.

The issues I am facing with this view are:

1. I am trying get a filter to display posts between two dates. Unfortunately it is not returning any results.
I am using the auto generated code:
<div class="form-group">
<label for="wpv-wpcf-funding-event-date_min">[wpml-string context="wpv-views"]From[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-funding-event-date" url_param="wpv-wpcf-funding-event-date_min"]
</div>
<div class="form-group">
<label for="wpv-wpcf-funding-event-date_max">[wpml-string context="wpv-views"]To[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-funding-event-date" url_param="wpv-wpcf-funding-event-date_max"]
</div>

2. I am also wanting to enable filtering by taxonomies of the parents. I have tried following the guidance provided by Luo here: https://toolset.com/forums/topic/parametric-search-with-parent-field/

I have modifed the code in that example but can't get it to work.

the code for the functions file I used was:

/**
* Custom code to enable filtering of funding events by Company-profile category
*/
add_filter( 'wpv_filter_query', 'filter_application_by_category_func', 10, 3 );
function filter_application_by_category_func( $query_args, $settings, $view_id ) {
if($view_id == 207 && isset($_GET['category']) && !empty($_GET['category'])){
$args = array(
'post_type' => 'company-profile',
'fields' => 'ids',
'meta_key' => 'wpcf-category',
'meta_value' => $_GET['category'],
'meta_compare' => 'IN',
);
$parent_ids = get_posts( $args );
$parent_ids[] = 0;
if(!isset($query_args['meta_query'])){
$query_args['meta_query'] = array();
}
$query_args['meta_query'][] = array(
'key' => '_wpcf_belongs_funding-event_id',
'value' => $parent_ids,
'compare' => 'IN',
);
}
return $query_args;
}

I inserted the following in the view:

[wpml-string context="wpv-views"]Category:[/wpml-string]
[wpv-control-post-taxonomy taxonomy="category" url_param="category" type="select" auto_fill="wpcf-category" auto_fill_sort="asc" auto_fill_default="All"]

Not sure what I am doing wrong.

Could you please help?

Cheers

Peter

#1715603

Hi Peter,

Thank you for contacting us and I'd be happy to assist.

Although 1-1 code customization is beyond the scope of support that we provide over the forum, we do our best to guide in the right direction, whenever possible.

To reproduce and troubleshoot these requirements on my test website, I'll need to see exactly how everything is set up on your website.

Can you please share temporary admin login details, along with the link to a page where this parametric search can be seen?

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1718903

Hi Waqar

I hope you had a good weekend.

I wonder how you are going with this.

Cheers

Peter

#1719663

Hi Peter,

Thank you for sharing these details.

I apologize for the delay in getting back on this, as we had an unusually busy forum queue before the weekend.

I tried to log in to the admin area, using the username and the password that you shared, but it is not being accepted.

Would you mind checking the username and the password again?

Note: I've set your next reply as private again.

regards,
Waqar

#1721535

Hi Waqar.

Just checking in to see how you are going. I hope my login details worked this time and to see how you were going looking at the search

Cheers

Peter

#1721797

Hi Peter,

Thank you for the updated access details and I can access the admin area now.

It is a fairly complicated requirement so this testing and research will take some time. But, I'll share my findings and recommendations as soon as I can.

Thank you for your patience.

regards,
Waqar

#1726319

Waqar

Thought I would check in again and see how you were going with my support request.

Cheers

Peter

#1726423

Hi Peter,

Thanks for checking in and sorry about the delay in getting back on this.

I'll be completing my testing around this and will share my findings with you by end of the day today.

regards,
Waqar

#1728857

Hi Peter,

Thank you for waiting and I apologize as this troubleshooting is taking longer than originally anticipated.

The challenge I'm facing between your website and my test website is that yours is using the older migrated post-relationships.

The suggested code snippet from Luo's other ticket doesn't exactly work in this case, since that ticket's requirement involved filtering by parent's custom field, while you need to filter by the parent's taxonomy.

1. The filter for the from and to date fields has been fixed and the query filter comparison type needed to be changed to "number", instead of the "DATE".
( screenshot: hidden link )

You can read the details about how the date field filters work, from this guide:
https://toolset.com/documentation/user-guides/views/date-filters/

2. As for filtering by parent's taxonomy filter, I'm getting some conflicting results, even when using the same code snippet on your website and mine.

To investigate this difference safely, I'll need the FTP access to your website and unfortunately, the FTP access details from your earlier message are not being accepted with the "Authentication failed." error.

Can you please check the FTP access details and share them again?
(I've set your next reply as private)

regards,
Waqar

#1732731

Hi Peter,

Thank you for sharing the updated sftp details.

Durint testing, I was able to make the filter for the parent taxonomy work, using the following code:


add_filter( 'wpv_filter_query', 'filter_emp_date_custom_fn', 1000 , 3 );
function filter_emp_date_custom_fn( $query_args, $view_settings ) {
    // check for specific view
    if (  ( !is_admin() && isset($view_settings['view_id'] ) ) && ( $view_settings['view_id'] == 62150 )  ) {
        // check if category filter is used
        if( (!empty($_GET['category'])) && ($_GET['category'] != '0') ) {
            // remove the default category taxonomy filter
            for ($i=0; $i < (count($query_args['tax_query']) - 1) ; $i++) {
                if ( $query_args['tax_query'][$i]['taxonomy'] == "category") {
                    unset($query_args['tax_query'][$i]);
                }   
            }

            // get all company-profile posts with selected category
            $args = array(
                'post_type' => 'company-profile',
                'posts_per_page' => -1,
                'fields' => 'ids',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'category',
                        'field'    => 'slug',
                        'terms'    => $_GET['category'],
                    ),
                ),
            );
            $parent_ids = get_posts( $args );

            // execute if some company-profile posts with selected category are found
            if(!empty($parent_ids)) {
                // get related Funding Events posts of the found company-profile posts
                foreach ($parent_ids as $key => $value) {
                    $query_by_element = $value; // ID of post to get relationship from
                    $relationship = 'company-profile_funding-event'; // relationship slug
                    $query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
                    $limit = 1000; // defaults
                    $offset = 0; // defaults
                    $args = array(); //nothing needed
                    $return = 'post_id'; // We want Post ID
                    $role_name_to_return = 'child'; // We want children.
               
                    $get_results = toolset_get_related_posts(
                                    $query_by_element,
                                    $relationship,
                                    $query_by_role_name,
                                    $limit,
                                    $offset,
                                    $args,
                                    $return,
                                    $role_name_to_return
                                    );
                    // if some Funding Events posts are found add them in an array $post_in
                    if(!empty($get_results)) {
                        foreach ($get_results as $get_result) {
                            $post_in[] = $get_result;
                        }
                    }          
                }

                if(!empty($post_in)) {
                    $query_args['post__in'] = $post_in;
                }
            }
        }
    }

    return $query_args;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

It gets the selected term for the "category" taxonomy and restricts the view's query to only bring in those "Funding Events" posts that are related to the "company-profile" posts attached to that selected term.

Important note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1732865

Thank you so much Waqar. I will do some testing and come back to you if I need anything clarification.

#1733085

You're very welcome Peter and I'll wait to hear back from you.

#1733809

Waqar

I am very pleased that you could get the category filter working.

I am trying now to get a second taxonomy of "company-profile" working, namely "company-state"

I have added a "company-state" filter into the view. but the code to insert in the functions.php file has be stumped.

I tried duplicating the code you provided replacing references to "category" with "company-state" with no luck.. I couldn't save the functions file.... I also tried inserting "company-state" into the code provided .. again I couldn't figure out how to make it work.

Could you please suggest how I can achieve this?

Cheers

Peter

#1734335

Hi Peter,

Thanks for the update and glad that the category filter is working.

If you're having difficultly in extending that code snippet to include another taxonomy filter, I'll recommend hiring a professional from our list of contractors:
https://toolset.com/contractors/

As mentioned earlier, the custom code examples from our forum are shared to get you started in the right direction and 1-1 customization assistance is beyond the scope of support that we can provide over the forum.
( ref: https://toolset.com/toolset-support-policy/ )

I hope you'll understand.

regards,
Waqar